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/debug/trace_event.h"
11 #include "base/logging.h"
12 #include "content/browser/child_process_security_policy_impl.h"
13 #include "content/browser/devtools/render_view_devtools_agent_host.h"
14 #include "content/browser/frame_host/interstitial_page_impl.h"
15 #include "content/browser/frame_host/navigation_controller_impl.h"
16 #include "content/browser/frame_host/navigation_entry_impl.h"
17 #include "content/browser/frame_host/render_frame_host_factory.h"
18 #include "content/browser/frame_host/render_frame_host_impl.h"
19 #include "content/browser/renderer_host/render_process_host_impl.h"
20 #include "content/browser/renderer_host/render_view_host_factory.h"
21 #include "content/browser/renderer_host/render_view_host_impl.h"
22 #include "content/browser/site_instance_impl.h"
23 #include "content/browser/webui/web_ui_controller_factory_registry.h"
24 #include "content/browser/webui/web_ui_impl.h"
25 #include "content/common/view_messages.h"
26 #include "content/port/browser/render_widget_host_view_port.h"
27 #include "content/public/browser/content_browser_client.h"
28 #include "content/public/browser/notification_service.h"
29 #include "content/public/browser/notification_types.h"
30 #include "content/public/browser/render_widget_host_iterator.h"
31 #include "content/public/browser/user_metrics.h"
32 #include "content/public/browser/web_ui_controller.h"
33 #include "content/public/common/content_switches.h"
34 #include "content/public/common/url_constants.h"
38 RenderFrameHostManager::PendingNavigationParams::PendingNavigationParams()
39 : is_transfer(false), frame_id(-1), should_replace_current_entry(false) {
42 RenderFrameHostManager::PendingNavigationParams::PendingNavigationParams(
43 const GlobalRequestID
& global_request_id
,
45 const std::vector
<GURL
>& transfer_url_chain
,
47 PageTransition page_transition
,
49 bool should_replace_current_entry
)
50 : global_request_id(global_request_id
),
51 is_transfer(is_transfer
),
52 transfer_url_chain(transfer_url_chain
),
54 page_transition(page_transition
),
56 should_replace_current_entry(should_replace_current_entry
) {
59 RenderFrameHostManager::PendingNavigationParams::~PendingNavigationParams() {}
61 RenderFrameHostManager::RenderFrameHostManager(
62 FrameTreeNode
* frame_tree_node
,
63 RenderFrameHostDelegate
* render_frame_delegate
,
64 RenderViewHostDelegate
* render_view_delegate
,
65 RenderWidgetHostDelegate
* render_widget_delegate
,
67 : frame_tree_node_(frame_tree_node
),
69 cross_navigation_pending_(false),
70 render_frame_delegate_(render_frame_delegate
),
71 render_view_delegate_(render_view_delegate
),
72 render_widget_delegate_(render_widget_delegate
),
73 render_frame_host_(NULL
),
74 pending_render_frame_host_(NULL
),
75 interstitial_page_(NULL
) {
78 RenderFrameHostManager::~RenderFrameHostManager() {
79 if (pending_render_frame_host_
)
82 // We should always have a current RenderFrameHost except in some tests.
83 // TODO(creis): Now that we aren't using Shutdown, make render_frame_host_ and
84 // RenderFrameHostMap use scoped_ptrs.
85 RenderFrameHostImpl
* render_frame_host
= render_frame_host_
;
86 render_frame_host_
= NULL
;
87 if (render_frame_host
)
88 delete render_frame_host
;
90 // Delete any swapped out RenderFrameHosts.
91 for (RenderFrameHostMap::iterator iter
= swapped_out_hosts_
.begin();
92 iter
!= swapped_out_hosts_
.end();
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 // TODO(creis): Make render_frame_host_ a scoped_ptr.
109 render_frame_host_
= CreateRenderFrameHost(site_instance
, view_routing_id
,
110 frame_routing_id
, false,
111 delegate_
->IsHidden());
113 // Keep track of renderer processes as they start to shut down or are
115 registrar_
.Add(this, NOTIFICATION_RENDERER_PROCESS_CLOSED
,
116 NotificationService::AllSources());
117 registrar_
.Add(this, NOTIFICATION_RENDERER_PROCESS_CLOSING
,
118 NotificationService::AllSources());
121 RenderViewHostImpl
* RenderFrameHostManager::current_host() const {
122 if (!render_frame_host_
)
124 return render_frame_host_
->render_view_host();
127 RenderViewHostImpl
* RenderFrameHostManager::pending_render_view_host() const {
128 if (!pending_render_frame_host_
)
130 return pending_render_frame_host_
->render_view_host();
133 RenderWidgetHostView
* RenderFrameHostManager::GetRenderWidgetHostView() const {
134 if (interstitial_page_
)
135 return interstitial_page_
->GetView();
136 if (!render_frame_host_
)
138 return render_frame_host_
->render_view_host()->GetView();
141 void RenderFrameHostManager::SetPendingWebUI(const NavigationEntryImpl
& entry
) {
142 pending_web_ui_
.reset(
143 delegate_
->CreateWebUIForRenderManager(entry
.GetURL()));
144 pending_and_current_web_ui_
.reset();
146 // If we have assigned (zero or more) bindings to this NavigationEntry in the
147 // past, make sure we're not granting it different bindings than it had
148 // before. If so, note it and don't give it any bindings, to avoid a
149 // potential privilege escalation.
150 if (pending_web_ui_
.get() &&
151 entry
.bindings() != NavigationEntryImpl::kInvalidBindings
&&
152 pending_web_ui_
->GetBindings() != entry
.bindings()) {
153 RecordAction(UserMetricsAction("ProcessSwapBindingsMismatch_RVHM"));
154 pending_web_ui_
.reset();
158 RenderViewHostImpl
* RenderFrameHostManager::Navigate(
159 const NavigationEntryImpl
& entry
) {
160 TRACE_EVENT0("browser", "RenderFrameHostManager:Navigate");
161 // Create a pending RenderFrameHost to use for the navigation.
162 RenderFrameHostImpl
* dest_render_frame_host
=
163 UpdateRendererStateForNavigate(entry
);
164 if (!dest_render_frame_host
)
165 return NULL
; // We weren't able to create a pending render frame host.
167 // If the current render_frame_host_ isn't live, we should create it so
168 // that we don't show a sad tab while the dest_render_frame_host fetches
169 // its first page. (Bug 1145340)
170 if (dest_render_frame_host
!= render_frame_host_
&&
171 !render_frame_host_
->render_view_host()->IsRenderViewLive()) {
172 // Note: we don't call InitRenderView here because we are navigating away
173 // soon anyway, and we don't have the NavigationEntry for this host.
174 delegate_
->CreateRenderViewForRenderManager(
175 render_frame_host_
->render_view_host(), MSG_ROUTING_NONE
);
178 // If the renderer crashed, then try to create a new one to satisfy this
179 // navigation request.
180 if (!dest_render_frame_host
->render_view_host()->IsRenderViewLive()) {
181 // Recreate the opener chain.
182 int opener_route_id
= delegate_
->CreateOpenerRenderViewsForRenderManager(
183 dest_render_frame_host
->render_view_host()->GetSiteInstance());
184 if (!InitRenderView(dest_render_frame_host
->render_view_host(),
188 // Now that we've created a new renderer, be sure to hide it if it isn't
189 // our primary one. Otherwise, we might crash if we try to call Show()
191 if (dest_render_frame_host
!= render_frame_host_
&&
192 dest_render_frame_host
->render_view_host()->GetView()) {
193 dest_render_frame_host
->render_view_host()->GetView()->Hide();
194 } else if (frame_tree_node_
->IsMainFrame()) {
195 // This is our primary renderer, notify here as we won't be calling
196 // CommitPending (which does the notify). We only do this for top-level
198 delegate_
->NotifySwappedFromRenderManager(
199 NULL
, render_frame_host_
->render_view_host());
203 // TODO(creis): Return the RFH instead, once we can navigate RFHs.
204 return dest_render_frame_host
->render_view_host();
207 void RenderFrameHostManager::Stop() {
208 render_frame_host_
->render_view_host()->Stop();
210 // If we are cross-navigating, we should stop the pending renderers. This
211 // will lead to a DidFailProvisionalLoad, which will properly destroy them.
212 if (cross_navigation_pending_
) {
213 pending_render_frame_host_
->render_view_host()->Send(new ViewMsg_Stop(
214 pending_render_frame_host_
->render_view_host()->GetRoutingID()));
218 void RenderFrameHostManager::SetIsLoading(bool is_loading
) {
219 render_frame_host_
->render_view_host()->SetIsLoading(is_loading
);
220 if (pending_render_frame_host_
)
221 pending_render_frame_host_
->render_view_host()->SetIsLoading(is_loading
);
224 bool RenderFrameHostManager::ShouldCloseTabOnUnresponsiveRenderer() {
225 if (!cross_navigation_pending_
)
228 // We should always have a pending RFH when there's a cross-process navigation
229 // in progress. Sanity check this for http://crbug.com/276333.
230 CHECK(pending_render_frame_host_
);
232 // If the tab becomes unresponsive during {before}unload while doing a
233 // cross-site navigation, proceed with the navigation. (This assumes that
234 // the pending RenderFrameHost is still responsive.)
235 if (render_frame_host_
->render_view_host()->is_waiting_for_unload_ack()) {
236 // The request has been started and paused while we're waiting for the
237 // unload handler to finish. We'll pretend that it did. The pending
238 // renderer will then be swapped in as part of the usual DidNavigate logic.
239 // (If the unload handler later finishes, this call will be ignored because
240 // the pending_nav_params_ state will already be cleaned up.)
241 current_host()->OnSwappedOut(true);
242 } else if (render_frame_host_
->render_view_host()->
243 is_waiting_for_beforeunload_ack()) {
244 // Haven't gotten around to starting the request, because we're still
245 // waiting for the beforeunload handler to finish. We'll pretend that it
246 // did finish, to let the navigation proceed. Note that there's a danger
247 // that the beforeunload handler will later finish and possibly return
248 // false (meaning the navigation should not proceed), but we'll ignore it
249 // in this case because it took too long.
250 if (pending_render_frame_host_
->render_view_host()->
251 are_navigations_suspended()) {
252 pending_render_frame_host_
->render_view_host()->SetNavigationsSuspended(
253 false, base::TimeTicks::Now());
259 // TODO(creis): Remove this in favor of SwappedOutFrame.
260 void RenderFrameHostManager::SwappedOut(RenderViewHost
* render_view_host
) {
261 // Make sure this is from our current RVH, and that we have a pending
262 // navigation from OnCrossSiteResponse. (There may be no pending navigation
263 // for data URLs that don't make network requests, for example.) If not,
264 // just return early and ignore.
265 if (render_view_host
!= render_frame_host_
->render_view_host() ||
266 !pending_nav_params_
.get()) {
267 pending_nav_params_
.reset();
271 // Now that the unload handler has run, we need to either initiate the
272 // pending transfer (if there is one) or resume the paused response (if not).
273 // TODO(creis): The blank swapped out page is visible during this time, but
274 // we can shorten this by delivering the response directly, rather than
275 // forcing an identical request to be made.
276 if (pending_nav_params_
->is_transfer
) {
277 // Treat the last URL in the chain as the destination and the remainder as
278 // the redirect chain.
279 CHECK(pending_nav_params_
->transfer_url_chain
.size());
280 GURL transfer_url
= pending_nav_params_
->transfer_url_chain
.back();
281 pending_nav_params_
->transfer_url_chain
.pop_back();
283 // We don't know whether the original request had |user_action| set to true.
284 // However, since we force the navigation to be in the current tab, it
286 render_view_host
->GetDelegate()->RequestTransferURL(
288 pending_nav_params_
->transfer_url_chain
,
289 pending_nav_params_
->referrer
,
290 pending_nav_params_
->page_transition
,
292 pending_nav_params_
->frame_id
,
293 pending_nav_params_
->global_request_id
,
294 pending_nav_params_
->should_replace_current_entry
,
296 } else if (pending_render_frame_host_
) {
297 RenderProcessHostImpl
* pending_process
=
298 static_cast<RenderProcessHostImpl
*>(
299 pending_render_frame_host_
->GetProcess());
300 pending_process
->ResumeDeferredNavigation(
301 pending_nav_params_
->global_request_id
);
303 pending_nav_params_
.reset();
306 void RenderFrameHostManager::SwappedOutFrame(
307 RenderFrameHostImpl
* render_frame_host
) {
308 // Make sure this is from our current RFH, and that we have a pending
309 // navigation from OnCrossSiteResponse. (There may be no pending navigation
310 // for data URLs that don't make network requests, for example.) If not,
311 // just return early and ignore.
312 if (render_frame_host
!= render_frame_host_
|| !pending_nav_params_
.get()) {
313 pending_nav_params_
.reset();
317 // Sanity check that this is for the correct frame.
318 DCHECK_EQ(frame_tree_node_
->frame_id(), pending_nav_params_
->frame_id
);
320 // Now that the unload handler has run, we need to either initiate the
321 // pending transfer (if there is one) or resume the paused response (if not).
322 // TODO(creis): The blank swapped out page is visible during this time, but
323 // we can shorten this by delivering the response directly, rather than
324 // forcing an identical request to be made.
325 if (pending_nav_params_
->is_transfer
) {
326 // Treat the last URL in the chain as the destination and the remainder as
327 // the redirect chain.
328 CHECK(pending_nav_params_
->transfer_url_chain
.size());
329 GURL transfer_url
= pending_nav_params_
->transfer_url_chain
.back();
330 pending_nav_params_
->transfer_url_chain
.pop_back();
332 // We don't know whether the original request had |user_action| set to true.
333 // However, since we force the navigation to be in the current tab, it
335 // TODO(creis): Move RequestTransferURL to RenderFrameHost's navigator.
336 render_frame_host
->render_view_host()->GetDelegate()->RequestTransferURL(
338 pending_nav_params_
->transfer_url_chain
,
339 pending_nav_params_
->referrer
,
340 pending_nav_params_
->page_transition
,
342 pending_nav_params_
->frame_id
,
343 pending_nav_params_
->global_request_id
,
346 } else if (pending_render_frame_host_
) {
347 RenderProcessHostImpl
* pending_process
=
348 static_cast<RenderProcessHostImpl
*>(
349 pending_render_frame_host_
->GetProcess());
350 pending_process
->ResumeDeferredNavigation(
351 pending_nav_params_
->global_request_id
);
353 pending_nav_params_
.reset();
356 // TODO(creis): This should take in a RenderFrameHost.
357 void RenderFrameHostManager::DidNavigateMainFrame(
358 RenderViewHost
* render_view_host
) {
359 if (!cross_navigation_pending_
) {
360 DCHECK(!pending_render_frame_host_
);
362 // We should only hear this from our current renderer.
363 DCHECK(render_view_host
== render_frame_host_
->render_view_host());
365 // Even when there is no pending RVH, there may be a pending Web UI.
366 if (pending_web_ui())
371 if (render_view_host
== pending_render_frame_host_
->render_view_host()) {
372 // The pending cross-site navigation completed, so show the renderer.
373 // If it committed without sending network requests (e.g., data URLs),
374 // then we still need to swap out the old RFH first and run its unload
375 // handler. OK for that to happen in the background.
376 if (pending_render_frame_host_
->render_view_host()->
377 HasPendingCrossSiteRequest())
381 cross_navigation_pending_
= false;
382 } else if (render_view_host
== render_frame_host_
->render_view_host()) {
383 // A navigation in the original page has taken place. Cancel the pending
386 cross_navigation_pending_
= false;
388 // No one else should be sending us DidNavigate in this state.
393 // TODO(creis): Take in RenderFrameHost instead, since frames can have openers.
394 void RenderFrameHostManager::DidDisownOpener(RenderViewHost
* render_view_host
) {
395 // Notify all swapped out hosts, including the pending RVH.
396 for (RenderFrameHostMap::iterator iter
= swapped_out_hosts_
.begin();
397 iter
!= swapped_out_hosts_
.end();
399 DCHECK_NE(iter
->second
->render_view_host()->GetSiteInstance(),
400 current_host()->GetSiteInstance());
401 iter
->second
->render_view_host()->DisownOpener();
405 void RenderFrameHostManager::RendererProcessClosing(
406 RenderProcessHost
* render_process_host
) {
407 // Remove any swapped out RVHs from this process, so that we don't try to
408 // swap them back in while the process is exiting. Start by finding them,
409 // since there could be more than one.
410 std::list
<int> ids_to_remove
;
411 for (RenderFrameHostMap::iterator iter
= swapped_out_hosts_
.begin();
412 iter
!= swapped_out_hosts_
.end();
414 if (iter
->second
->GetProcess() == render_process_host
)
415 ids_to_remove
.push_back(iter
->first
);
419 while (!ids_to_remove
.empty()) {
420 delete swapped_out_hosts_
[ids_to_remove
.back()];
421 swapped_out_hosts_
.erase(ids_to_remove
.back());
422 ids_to_remove
.pop_back();
426 void RenderFrameHostManager::ShouldClosePage(
427 bool for_cross_site_transition
,
429 const base::TimeTicks
& proceed_time
) {
430 if (for_cross_site_transition
) {
431 // Ignore if we're not in a cross-site navigation.
432 if (!cross_navigation_pending_
)
436 // Ok to unload the current page, so proceed with the cross-site
437 // navigation. Note that if navigations are not currently suspended, it
438 // might be because the renderer was deemed unresponsive and this call was
439 // already made by ShouldCloseTabOnUnresponsiveRenderer. In that case, it
440 // is ok to do nothing here.
441 if (pending_render_frame_host_
&&
442 pending_render_frame_host_
->render_view_host()->
443 are_navigations_suspended()) {
444 pending_render_frame_host_
->render_view_host()->
445 SetNavigationsSuspended(false, proceed_time
);
448 // Current page says to cancel.
450 cross_navigation_pending_
= false;
453 // Non-cross site transition means closing the entire tab.
454 bool proceed_to_fire_unload
;
455 delegate_
->BeforeUnloadFiredFromRenderManager(proceed
, proceed_time
,
456 &proceed_to_fire_unload
);
458 if (proceed_to_fire_unload
) {
459 // If we're about to close the tab and there's a pending RFH, cancel it.
460 // Otherwise, if the navigation in the pending RFH completes before the
461 // close in the current RFH, we'll lose the tab close.
462 if (pending_render_frame_host_
) {
464 cross_navigation_pending_
= false;
467 // This is not a cross-site navigation, the tab is being closed.
468 render_frame_host_
->render_view_host()->ClosePage();
473 // TODO(creis): Take in a RenderFrameHost from CSRH.
474 void RenderFrameHostManager::OnCrossSiteResponse(
475 RenderViewHost
* pending_render_view_host
,
476 const GlobalRequestID
& global_request_id
,
478 const std::vector
<GURL
>& transfer_url_chain
,
479 const Referrer
& referrer
,
480 PageTransition page_transition
,
482 bool should_replace_current_entry
) {
483 // This should be called either when the pending RVH is ready to commit or
484 // when we realize that the current RVH's request requires a transfer.
485 DCHECK(pending_render_view_host
== render_frame_host_
->render_view_host() ||
486 pending_render_view_host
==
487 pending_render_frame_host_
->render_view_host());
489 // TODO(creis): Eventually we will want to check all navigation responses
490 // here, but currently we pass information for a transfer if
491 // ShouldSwapProcessesForRedirect returned true in the network stack.
492 // In that case, we should set up a transfer after the unload handler runs.
493 // If is_transfer is false, we will just run the unload handler and resume.
494 pending_nav_params_
.reset(new PendingNavigationParams(
495 global_request_id
, is_transfer
, transfer_url_chain
, referrer
,
496 page_transition
, frame_id
, should_replace_current_entry
));
498 // Run the unload handler of the current page.
502 void RenderFrameHostManager::SwapOutOldPage() {
503 // Should only see this while we have a pending renderer or transfer.
504 CHECK(cross_navigation_pending_
|| pending_nav_params_
.get());
506 // Tell the renderer to suppress any further modal dialogs so that we can swap
507 // it out. This must be done before canceling any current dialog, in case
508 // there is a loop creating additional dialogs.
509 render_frame_host_
->render_view_host()->SuppressDialogsUntilSwapOut();
511 // Now close any modal dialogs that would prevent us from swapping out. This
512 // must be done separately from SwapOut, so that the PageGroupLoadDeferrer is
513 // no longer on the stack when we send the SwapOut message.
514 delegate_
->CancelModalDialogsForRenderManager();
516 // Tell the old renderer it is being swapped out. This will fire the unload
517 // handler (without firing the beforeunload handler a second time). When the
518 // unload handler finishes and the navigation completes, we will send a
519 // message to the ResourceDispatcherHost, allowing the pending RVH's response
521 // Note: This must be done on the RFH or else we'll swap out the top-level
522 // page when subframes navigate.
523 if (frame_tree_node_
->IsMainFrame())
524 render_frame_host_
->render_view_host()->SwapOut();
526 render_frame_host_
->SwapOut();
528 // ResourceDispatcherHost has told us to run the onunload handler, which
529 // means it is not a download or unsafe page, and we are going to perform the
530 // navigation. Thus, we no longer need to remember that the RenderFrameHost
531 // is part of a pending cross-site request.
532 if (pending_render_frame_host_
) {
533 pending_render_frame_host_
->render_view_host()->
534 SetHasPendingCrossSiteRequest(false);
538 void RenderFrameHostManager::Observe(
540 const NotificationSource
& source
,
541 const NotificationDetails
& details
) {
543 case NOTIFICATION_RENDERER_PROCESS_CLOSED
:
544 case NOTIFICATION_RENDERER_PROCESS_CLOSING
:
545 RendererProcessClosing(
546 Source
<RenderProcessHost
>(source
).ptr());
554 bool RenderFrameHostManager::ClearSwappedOutRFHsInSiteInstance(
555 int32 site_instance_id
,
556 FrameTreeNode
* node
) {
557 RenderFrameHostMap::iterator iter
=
558 node
->render_manager()->swapped_out_hosts_
.find(site_instance_id
);
559 if (iter
!= node
->render_manager()->swapped_out_hosts_
.end())
565 bool RenderFrameHostManager::ShouldTransitionCrossSite() {
566 // False in the single-process mode, as it makes RVHs to accumulate
567 // in swapped_out_hosts_.
568 // True if we are using process-per-site-instance (default) or
569 // process-per-site (kProcessPerSite).
571 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess
) &&
572 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kProcessPerTab
);
575 bool RenderFrameHostManager::ShouldSwapBrowsingInstancesForNavigation(
576 const NavigationEntry
* current_entry
,
577 const NavigationEntryImpl
* new_entry
) const {
580 // If new_entry already has a SiteInstance, assume it is correct and use it.
581 if (new_entry
->site_instance())
584 // Check for reasons to swap processes even if we are in a process model that
585 // doesn't usually swap (e.g., process-per-tab). Any time we return true,
586 // the new_entry will be rendered in a new SiteInstance AND BrowsingInstance.
588 // We use the effective URL here, since that's what is used in the
589 // SiteInstance's site and when we later call IsSameWebSite. If there is no
590 // current_entry, check the current SiteInstance's site, which might already
591 // be committed to a Web UI URL (such as the NTP).
592 BrowserContext
* browser_context
=
593 delegate_
->GetControllerForRenderManager().GetBrowserContext();
594 const GURL
& current_url
= (current_entry
) ?
595 SiteInstanceImpl::GetEffectiveURL(browser_context
,
596 current_entry
->GetURL()) :
597 render_frame_host_
->render_view_host()->GetSiteInstance()->GetSiteURL();
598 const GURL
& new_url
= SiteInstanceImpl::GetEffectiveURL(browser_context
,
599 new_entry
->GetURL());
601 // For security, we should transition between processes when one is a Web UI
602 // page and one isn't.
603 if (WebUIControllerFactoryRegistry::GetInstance()->UseWebUIForURL(
604 browser_context
, current_url
)) {
605 // If so, force a swap if destination is not an acceptable URL for Web UI.
606 // Here, data URLs are never allowed.
607 if (!WebUIControllerFactoryRegistry::GetInstance()->IsURLAcceptableForWebUI(
608 browser_context
, new_url
, false)) {
612 // Force a swap if it's a Web UI URL.
613 if (WebUIControllerFactoryRegistry::GetInstance()->UseWebUIForURL(
614 browser_context
, new_url
)) {
619 // Check with the content client as well. Important to pass current_url here,
620 // which uses the SiteInstance's site if there is no current_entry.
621 if (GetContentClient()->browser()->ShouldSwapBrowsingInstancesForNavigation(
622 render_frame_host_
->render_view_host()->GetSiteInstance(),
623 current_url
, new_url
)) {
627 // We can't switch a RenderView between view source and non-view source mode
628 // without screwing up the session history sometimes (when navigating between
629 // "view-source:http://foo.com/" and "http://foo.com/", Blink doesn't treat
630 // it as a new navigation). So require a BrowsingInstance switch.
632 current_entry
->IsViewSourceMode() != new_entry
->IsViewSourceMode())
638 bool RenderFrameHostManager::ShouldReuseWebUI(
639 const NavigationEntry
* current_entry
,
640 const NavigationEntryImpl
* new_entry
) const {
641 NavigationControllerImpl
& controller
=
642 delegate_
->GetControllerForRenderManager();
643 return current_entry
&& web_ui_
.get() &&
644 (WebUIControllerFactoryRegistry::GetInstance()->GetWebUIType(
645 controller
.GetBrowserContext(), current_entry
->GetURL()) ==
646 WebUIControllerFactoryRegistry::GetInstance()->GetWebUIType(
647 controller
.GetBrowserContext(), new_entry
->GetURL()));
650 SiteInstance
* RenderFrameHostManager::GetSiteInstanceForEntry(
651 const NavigationEntryImpl
& entry
,
652 SiteInstance
* current_instance
,
653 bool force_browsing_instance_swap
) {
654 // Determine which SiteInstance to use for navigating to |entry|.
655 const GURL
& dest_url
= entry
.GetURL();
656 NavigationControllerImpl
& controller
=
657 delegate_
->GetControllerForRenderManager();
658 BrowserContext
* browser_context
= controller
.GetBrowserContext();
660 // If a swap is required, we need to force the SiteInstance AND
661 // BrowsingInstance to be different ones, using CreateForURL.
662 if (force_browsing_instance_swap
) {
663 // We shouldn't be forcing a swap if an entry already has a SiteInstance.
664 CHECK(!entry
.site_instance());
665 return SiteInstance::CreateForURL(browser_context
, dest_url
);
668 // If the entry has an instance already we should use it.
669 if (entry
.site_instance())
670 return entry
.site_instance();
672 // (UGLY) HEURISTIC, process-per-site only:
674 // If this navigation is generated, then it probably corresponds to a search
675 // query. Given that search results typically lead to users navigating to
676 // other sites, we don't really want to use the search engine hostname to
677 // determine the site instance for this navigation.
679 // NOTE: This can be removed once we have a way to transition between
680 // RenderViews in response to a link click.
682 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kProcessPerSite
) &&
683 PageTransitionCoreTypeIs(entry
.GetTransitionType(),
684 PAGE_TRANSITION_GENERATED
)) {
685 return current_instance
;
688 SiteInstanceImpl
* current_site_instance
=
689 static_cast<SiteInstanceImpl
*>(current_instance
);
691 // If we haven't used our SiteInstance (and thus RVH) yet, then we can use it
692 // for this entry. We won't commit the SiteInstance to this site until the
693 // navigation commits (in DidNavigate), unless the navigation entry was
694 // restored or it's a Web UI as described below.
695 if (!current_site_instance
->HasSite()) {
696 // If we've already created a SiteInstance for our destination, we don't
697 // want to use this unused SiteInstance; use the existing one. (We don't
698 // do this check if the current_instance has a site, because for now, we
699 // want to compare against the current URL and not the SiteInstance's site.
700 // In this case, there is no current URL, so comparing against the site is
701 // ok. See additional comments below.)
703 // Also, if the URL should use process-per-site mode and there is an
704 // existing process for the site, we should use it. We can call
705 // GetRelatedSiteInstance() for this, which will eagerly set the site and
706 // thus use the correct process.
707 bool use_process_per_site
=
708 RenderProcessHost::ShouldUseProcessPerSite(browser_context
, dest_url
) &&
709 RenderProcessHostImpl::GetProcessHostForSite(browser_context
, dest_url
);
710 if (current_site_instance
->HasRelatedSiteInstance(dest_url
) ||
711 use_process_per_site
) {
712 return current_site_instance
->GetRelatedSiteInstance(dest_url
);
715 // For extensions, Web UI URLs (such as the new tab page), and apps we do
716 // not want to use the current_instance if it has no site, since it will
717 // have a RenderProcessHost of PRIV_NORMAL. Create a new SiteInstance for
718 // this URL instead (with the correct process type).
719 if (current_site_instance
->HasWrongProcessForURL(dest_url
))
720 return current_site_instance
->GetRelatedSiteInstance(dest_url
);
722 // View-source URLs must use a new SiteInstance and BrowsingInstance.
723 // TODO(nasko): This is the same condition as later in the function. This
724 // should be taken into account when refactoring this method as part of
725 // http://crbug.com/123007.
726 if (entry
.IsViewSourceMode())
727 return SiteInstance::CreateForURL(browser_context
, dest_url
);
729 // If we are navigating from a blank SiteInstance to a WebUI, make sure we
730 // create a new SiteInstance.
731 if (WebUIControllerFactoryRegistry::GetInstance()->UseWebUIForURL(
732 browser_context
, dest_url
)) {
733 return SiteInstance::CreateForURL(browser_context
, dest_url
);
736 // Normally the "site" on the SiteInstance is set lazily when the load
737 // actually commits. This is to support better process sharing in case
738 // the site redirects to some other site: we want to use the destination
739 // site in the site instance.
741 // In the case of session restore, as it loads all the pages immediately
742 // we need to set the site first, otherwise after a restore none of the
743 // pages would share renderers in process-per-site.
744 if (entry
.restore_type() != NavigationEntryImpl::RESTORE_NONE
)
745 current_site_instance
->SetSite(dest_url
);
747 return current_site_instance
;
750 // Otherwise, only create a new SiteInstance for a cross-site navigation.
752 // TODO(creis): Once we intercept links and script-based navigations, we
753 // will be able to enforce that all entries in a SiteInstance actually have
754 // the same site, and it will be safe to compare the URL against the
755 // SiteInstance's site, as follows:
756 // const GURL& current_url = current_instance->site();
757 // For now, though, we're in a hybrid model where you only switch
758 // SiteInstances if you type in a cross-site URL. This means we have to
759 // compare the entry's URL to the last committed entry's URL.
760 NavigationEntry
* current_entry
= controller
.GetLastCommittedEntry();
761 if (interstitial_page_
) {
762 // The interstitial is currently the last committed entry, but we want to
763 // compare against the last non-interstitial entry.
764 current_entry
= controller
.GetEntryAtOffset(-1);
766 // If there is no last non-interstitial entry (and current_instance already
767 // has a site), then we must have been opened from another tab. We want
768 // to compare against the URL of the page that opened us, but we can't
769 // get to it directly. The best we can do is check against the site of
770 // the SiteInstance. This will be correct when we intercept links and
771 // script-based navigations, but for now, it could place some pages in a
772 // new process unnecessarily. We should only hit this case if a page tries
773 // to open a new tab to an interstitial-inducing URL, and then navigates
774 // the page to a different same-site URL. (This seems very unlikely in
776 const GURL
& current_url
= (current_entry
) ? current_entry
->GetURL() :
777 current_instance
->GetSiteURL();
779 // View-source URLs must use a new SiteInstance and BrowsingInstance.
780 // TODO(creis): Refactor this method so this duplicated code isn't needed.
781 // See http://crbug.com/123007.
783 current_entry
->IsViewSourceMode() != entry
.IsViewSourceMode()) {
784 return SiteInstance::CreateForURL(browser_context
, dest_url
);
787 // Use the current SiteInstance for same site navigations, as long as the
788 // process type is correct. (The URL may have been installed as an app since
789 // the last time we visited it.)
790 if (SiteInstance::IsSameWebSite(browser_context
, current_url
, dest_url
) &&
791 !current_site_instance
->HasWrongProcessForURL(dest_url
)) {
792 return current_instance
;
795 // Start the new renderer in a new SiteInstance, but in the current
796 // BrowsingInstance. It is important to immediately give this new
797 // SiteInstance to a RenderViewHost (if it is different than our current
798 // SiteInstance), so that it is ref counted. This will happen in
800 return current_instance
->GetRelatedSiteInstance(dest_url
);
803 RenderFrameHostImpl
* RenderFrameHostManager::CreateRenderFrameHost(
804 SiteInstance
* site_instance
,
806 int frame_routing_id
,
809 if (frame_routing_id
== MSG_ROUTING_NONE
)
810 frame_routing_id
= site_instance
->GetProcess()->GetNextRoutingID();
812 // Create a RVH for main frames, or find the existing one for subframes.
813 FrameTree
* frame_tree
= frame_tree_node_
->frame_tree();
814 RenderViewHostImpl
* render_view_host
= NULL
;
815 if (frame_tree_node_
->IsMainFrame()) {
816 render_view_host
= frame_tree
->CreateRenderViewHostForMainFrame(
817 site_instance
, view_routing_id
, frame_routing_id
, swapped_out
, hidden
);
819 render_view_host
= frame_tree
->GetRenderViewHostForSubFrame(site_instance
);
821 // If we haven't found a RVH for a subframe RFH, it's because we currently
822 // do not create top-level RFHs for pending subframe navigations. Create
823 // the RVH here for now.
824 // TODO(creis): Mirror the frame tree so this check isn't necessary.
825 if (!render_view_host
) {
826 render_view_host
= frame_tree
->CreateRenderViewHostForMainFrame(
827 site_instance
, view_routing_id
, frame_routing_id
, swapped_out
,
832 // TODO(creis): Make render_frame_host a scoped_ptr.
833 // TODO(creis): Pass hidden to RFH.
834 RenderFrameHostImpl
* render_frame_host
=
835 RenderFrameHostFactory::Create(render_view_host
,
836 render_frame_delegate_
,
840 swapped_out
).release();
841 return render_frame_host
;
844 int RenderFrameHostManager::CreateRenderFrame(
845 SiteInstance
* instance
,
850 DCHECK(!swapped_out
|| hidden
); // Swapped out views should always be hidden.
852 // We are creating a pending or swapped out RFH here. We should never create
853 // it in the same SiteInstance as our current RFH.
854 CHECK_NE(render_frame_host_
->render_view_host()->GetSiteInstance(), instance
);
856 // Check if we've already created an RFH for this SiteInstance. If so, try
857 // to re-use the existing one, which has already been initialized. We'll
858 // remove it from the list of swapped out hosts if it commits.
859 RenderFrameHostImpl
* new_render_frame_host
=
860 GetSwappedOutRenderFrameHost(instance
);
861 if (new_render_frame_host
) {
862 // Prevent the process from exiting while we're trying to use it.
864 new_render_frame_host
->GetProcess()->AddPendingView();
866 // Create a new RenderFrameHost if we don't find an existing one.
867 // TODO(creis): Make new_render_frame_host a scoped_ptr.
868 new_render_frame_host
= CreateRenderFrameHost(instance
, MSG_ROUTING_NONE
,
869 MSG_ROUTING_NONE
, swapped_out
,
872 // If the new RFH is swapped out already, store it. Otherwise prevent the
873 // process from exiting while we're trying to navigate in it.
875 swapped_out_hosts_
[instance
->GetId()] = new_render_frame_host
;
877 new_render_frame_host
->GetProcess()->AddPendingView();
880 RenderViewHostImpl
* render_view_host
=
881 new_render_frame_host
->render_view_host();
882 bool success
= InitRenderView(render_view_host
, opener_route_id
);
883 if (success
&& frame_tree_node_
->IsMainFrame()) {
884 // Don't show the main frame's view until we get a DidNavigate from it.
885 render_view_host
->GetView()->Hide();
886 } else if (!swapped_out
&& pending_render_frame_host_
) {
891 // Use this as our new pending RFH if it isn't swapped out.
893 pending_render_frame_host_
= new_render_frame_host
;
895 return new_render_frame_host
->render_view_host()->GetRoutingID();
898 bool RenderFrameHostManager::InitRenderView(RenderViewHost
* render_view_host
,
899 int opener_route_id
) {
900 // We may have initialized this RenderViewHost for another RenderFrameHost.
901 if (render_view_host
->IsRenderViewLive())
904 // If the pending navigation is to a WebUI and the RenderView is not in a
905 // guest process, tell the RenderViewHost about any bindings it will need
907 if (pending_web_ui() && !render_view_host
->GetProcess()->IsGuest()) {
908 render_view_host
->AllowBindings(pending_web_ui()->GetBindings());
910 // Ensure that we don't create an unprivileged RenderView in a WebUI-enabled
911 // process unless it's swapped out.
912 RenderViewHostImpl
* rvh_impl
=
913 static_cast<RenderViewHostImpl
*>(render_view_host
);
914 if (!rvh_impl
->is_swapped_out()) {
915 CHECK(!ChildProcessSecurityPolicyImpl::GetInstance()->HasWebUIBindings(
916 render_view_host
->GetProcess()->GetID()));
920 return delegate_
->CreateRenderViewForRenderManager(render_view_host
,
924 void RenderFrameHostManager::CommitPending() {
925 // First check whether we're going to want to focus the location bar after
926 // this commit. We do this now because the navigation hasn't formally
927 // committed yet, so if we've already cleared |pending_web_ui_| the call chain
928 // this triggers won't be able to figure out what's going on.
929 bool will_focus_location_bar
= delegate_
->FocusLocationBarByDefault();
931 // We expect SwapOutOldPage to have canceled any modal dialogs and told the
932 // renderer to suppress any further dialogs until it is swapped out. However,
933 // crash reports indicate that it's still possible for modal dialogs to exist
934 // at this point, which poses a risk if we delete their RenderViewHost below.
935 // Cancel them again to be safe. http://crbug.com/324320.
936 delegate_
->CancelModalDialogsForRenderManager();
938 // Next commit the Web UI, if any. Either replace |web_ui_| with
939 // |pending_web_ui_|, or clear |web_ui_| if there is no pending WebUI, or
940 // leave |web_ui_| as is if reusing it.
941 DCHECK(!(pending_web_ui_
.get() && pending_and_current_web_ui_
.get()));
943 web_ui_
.reset(pending_web_ui_
.release());
944 else if (!pending_and_current_web_ui_
.get())
947 // It's possible for the pending_render_frame_host_ to be NULL when we aren't
948 // crossing process boundaries. If so, we just needed to handle the Web UI
949 // committing above and we're done.
950 if (!pending_render_frame_host_
) {
951 if (will_focus_location_bar
)
952 delegate_
->SetFocusToLocationBar(false);
956 // Remember if the page was focused so we can focus the new renderer in
958 bool focus_render_view
= !will_focus_location_bar
&&
959 render_frame_host_
->render_view_host()->GetView() &&
960 render_frame_host_
->render_view_host()->GetView()->HasFocus();
962 // TODO(creis): As long as show/hide are on RVH, we don't want to do them for
963 // subframe navigations or they'll interfere with the top-level page.
964 bool is_main_frame
= frame_tree_node_
->IsMainFrame();
966 // Swap in the pending frame and make it active. Also ensure the FrameTree
968 RenderFrameHostImpl
* old_render_frame_host
= render_frame_host_
;
969 render_frame_host_
= pending_render_frame_host_
;
970 pending_render_frame_host_
= NULL
;
972 render_frame_host_
->render_view_host()->AttachToFrameTree();
974 // The process will no longer try to exit, so we can decrement the count.
975 render_frame_host_
->GetProcess()->RemovePendingView();
977 // If the view is gone, then this RenderViewHost died while it was hidden.
978 // We ignored the RenderProcessGone call at the time, so we should send it now
979 // to make sure the sad tab shows up, etc.
980 if (!render_frame_host_
->render_view_host()->GetView()) {
981 delegate_
->RenderProcessGoneFromRenderManager(
982 render_frame_host_
->render_view_host());
983 } else if (!delegate_
->IsHidden() && is_main_frame
) {
984 render_frame_host_
->render_view_host()->GetView()->Show();
987 // If the old view is live and top-level, hide it now that the new one is
989 if (old_render_frame_host
->render_view_host()->GetView()) {
991 old_render_frame_host
->render_view_host()->GetView()->Hide();
992 old_render_frame_host
->render_view_host()->WasSwappedOut();
994 // TODO(creis): We'll need to set this back to false if we navigate back.
995 old_render_frame_host
->set_swapped_out(true);
999 // Make sure the size is up to date. (Fix for bug 1079768.)
1000 delegate_
->UpdateRenderViewSizeForRenderManager();
1002 if (will_focus_location_bar
) {
1003 delegate_
->SetFocusToLocationBar(false);
1004 } else if (focus_render_view
&&
1005 render_frame_host_
->render_view_host()->GetView()) {
1006 RenderWidgetHostViewPort::FromRWHV(
1007 render_frame_host_
->render_view_host()->GetView())->Focus();
1010 // Notify that we've swapped RenderFrameHosts. We do this before shutting down
1011 // the RFH so that we can clean up RendererResources related to the RFH first.
1012 // TODO(creis): Only do this on top-level RFHs for now, and later update it to
1014 if (is_main_frame
) {
1015 delegate_
->NotifySwappedFromRenderManager(
1016 old_render_frame_host
->render_view_host(),
1017 render_frame_host_
->render_view_host());
1020 // If the pending frame was on the swapped out list, we can remove it.
1021 swapped_out_hosts_
.erase(render_frame_host_
->render_view_host()->
1022 GetSiteInstance()->GetId());
1024 if (old_render_frame_host
->render_view_host()->IsRenderViewLive()) {
1025 // If the old RFH is live, we are swapping it out and should keep track of
1026 // it in case we navigate back to it.
1027 // TODO(creis): Swap out the subframe in --site-per-process.
1028 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kSitePerProcess
))
1029 DCHECK(old_render_frame_host
->is_swapped_out() ||
1030 old_render_frame_host
->render_view_host()->is_swapped_out());
1032 // Temp fix for http://crbug.com/90867 until we do a better cleanup to make
1033 // sure we don't get different rvh instances for the same site instance
1034 // in the same rvhmgr.
1035 // TODO(creis): Clean this up.
1036 int32 old_site_instance_id
=
1037 old_render_frame_host
->render_view_host()->GetSiteInstance()->GetId();
1038 RenderFrameHostMap::iterator iter
=
1039 swapped_out_hosts_
.find(old_site_instance_id
);
1040 if (iter
!= swapped_out_hosts_
.end() &&
1041 iter
->second
!= old_render_frame_host
) {
1042 // Delete the RFH that will be replaced in the map to avoid a leak.
1043 delete iter
->second
;
1045 swapped_out_hosts_
[old_site_instance_id
] = old_render_frame_host
;
1047 // If there are no active views in this SiteInstance, it means that
1048 // this RFH was the last active one in the SiteInstance. Now that we
1049 // know that all RFHs are swapped out, we can delete all the RFHs and RVHs
1050 // in this SiteInstance. We do this after ensuring the RFH is on the
1051 // swapped out list to simplify the deletion.
1052 if (!static_cast<SiteInstanceImpl
*>(
1053 old_render_frame_host
->render_view_host()->GetSiteInstance())->
1054 active_view_count()) {
1055 ShutdownRenderFrameHostsInSiteInstance(old_site_instance_id
);
1056 // This is deleted while cleaning up the SiteInstance's views.
1057 old_render_frame_host
= NULL
;
1060 delete old_render_frame_host
;
1064 void RenderFrameHostManager::ShutdownRenderFrameHostsInSiteInstance(
1065 int32 site_instance_id
) {
1066 // First remove any swapped out RFH for this SiteInstance from our own list.
1067 ClearSwappedOutRFHsInSiteInstance(site_instance_id
, frame_tree_node_
);
1069 // Use the safe RenderWidgetHost iterator for now to find all RenderViewHosts
1070 // in the SiteInstance, then tell their respective FrameTrees to remove all
1071 // swapped out RenderFrameHosts corresponding to them.
1072 // TODO(creis): Replace this with a RenderFrameHostIterator that protects
1073 // against use-after-frees if a later element is deleted before getting to it.
1074 scoped_ptr
<RenderWidgetHostIterator
> widgets(
1075 RenderWidgetHostImpl::GetAllRenderWidgetHosts());
1076 while (RenderWidgetHost
* widget
= widgets
->GetNextHost()) {
1077 if (!widget
->IsRenderView())
1079 RenderViewHostImpl
* rvh
=
1080 static_cast<RenderViewHostImpl
*>(RenderViewHost::From(widget
));
1081 if (site_instance_id
== rvh
->GetSiteInstance()->GetId()) {
1082 // This deletes all RenderFrameHosts using the |rvh|, which then causes
1083 // |rvh| to Shutdown.
1084 FrameTree
* tree
= rvh
->GetDelegate()->GetFrameTree();
1085 tree
->ForEach(base::Bind(
1086 &RenderFrameHostManager::ClearSwappedOutRFHsInSiteInstance
,
1088 // rvh is now deleted.
1093 RenderFrameHostImpl
* RenderFrameHostManager::UpdateRendererStateForNavigate(
1094 const NavigationEntryImpl
& entry
) {
1095 // If we are currently navigating cross-process, we want to get back to normal
1096 // and then navigate as usual.
1097 if (cross_navigation_pending_
) {
1098 if (pending_render_frame_host_
)
1100 cross_navigation_pending_
= false;
1103 // render_frame_host_'s SiteInstance and new_instance will not be deleted
1104 // before the end of this method, so we don't have to worry about their ref
1105 // counts dropping to zero.
1106 SiteInstance
* current_instance
=
1107 render_frame_host_
->render_view_host()->GetSiteInstance();
1108 SiteInstance
* new_instance
= current_instance
;
1110 // We do not currently swap processes for navigations in webview tag guests.
1111 bool is_guest_scheme
= current_instance
->GetSiteURL().SchemeIs(kGuestScheme
);
1113 // Determine if we need a new BrowsingInstance for this entry. If true, this
1114 // implies that it will get a new SiteInstance (and likely process), and that
1115 // other tabs in the current BrowsingInstance will be unable to script it.
1116 // This is used for cases that require a process swap even in the
1117 // process-per-tab model, such as WebUI pages.
1118 const NavigationEntry
* current_entry
=
1119 delegate_
->GetLastCommittedNavigationEntryForRenderManager();
1120 bool force_swap
= !is_guest_scheme
&&
1121 ShouldSwapBrowsingInstancesForNavigation(current_entry
, &entry
);
1122 if (!is_guest_scheme
&& (ShouldTransitionCrossSite() || force_swap
))
1123 new_instance
= GetSiteInstanceForEntry(entry
, current_instance
, force_swap
);
1125 // If force_swap is true, we must use a different SiteInstance. If we didn't,
1126 // we would have two RenderFrameHosts in the same SiteInstance and the same
1127 // frame, resulting in page_id conflicts for their NavigationEntries.
1129 CHECK_NE(new_instance
, current_instance
);
1131 if (new_instance
!= current_instance
) {
1132 // New SiteInstance: create a pending RFH to navigate.
1133 DCHECK(!cross_navigation_pending_
);
1135 // This will possibly create (set to NULL) a Web UI object for the pending
1136 // page. We'll use this later to give the page special access. This must
1137 // happen before the new renderer is created below so it will get bindings.
1138 // It must also happen after the above conditional call to CancelPending(),
1139 // otherwise CancelPending may clear the pending_web_ui_ and the page will
1140 // not have its bindings set appropriately.
1141 SetPendingWebUI(entry
);
1143 // Ensure that we have created RFHs for the new RFH's opener chain if
1144 // we are staying in the same BrowsingInstance. This allows the pending RFH
1145 // to send cross-process script calls to its opener(s).
1146 int opener_route_id
= MSG_ROUTING_NONE
;
1147 if (new_instance
->IsRelatedSiteInstance(current_instance
)) {
1149 delegate_
->CreateOpenerRenderViewsForRenderManager(new_instance
);
1152 // Create a non-swapped-out pending RFH with the given opener and navigate
1154 int route_id
= CreateRenderFrame(new_instance
, opener_route_id
, false,
1155 delegate_
->IsHidden());
1156 if (route_id
== MSG_ROUTING_NONE
)
1159 // Check if our current RFH is live before we set up a transition.
1160 if (!render_frame_host_
->render_view_host()->IsRenderViewLive()) {
1161 if (!cross_navigation_pending_
) {
1162 // The current RFH is not live. There's no reason to sit around with a
1163 // sad tab or a newly created RFH while we wait for the pending RFH to
1164 // navigate. Just switch to the pending RFH now and go back to non
1165 // cross-navigating (Note that we don't care about on{before}unload
1166 // handlers if the current RFH isn't live.)
1168 return render_frame_host_
;
1171 return render_frame_host_
;
1174 // Otherwise, it's safe to treat this as a pending cross-site transition.
1176 // We need to wait until the beforeunload handler has run, unless we are
1177 // transferring an existing request (in which case it has already run).
1178 // Suspend the new render view (i.e., don't let it send the cross-site
1179 // Navigate message) until we hear back from the old renderer's
1180 // beforeunload handler. If the handler returns false, we'll have to
1181 // cancel the request.
1182 DCHECK(!pending_render_frame_host_
->render_view_host()->
1183 are_navigations_suspended());
1185 entry
.transferred_global_request_id() != GlobalRequestID();
1187 // We don't need to stop the old renderer or run beforeunload/unload
1188 // handlers, because those have already been done.
1189 DCHECK(pending_nav_params_
->global_request_id
==
1190 entry
.transferred_global_request_id());
1192 // Also make sure the old render view stops, in case a load is in
1193 // progress. (We don't want to do this for transfers, since it will
1194 // interrupt the transfer with an unexpected DidStopLoading.)
1195 render_frame_host_
->render_view_host()->Send(new ViewMsg_Stop(
1196 render_frame_host_
->render_view_host()->GetRoutingID()));
1198 pending_render_frame_host_
->render_view_host()->SetNavigationsSuspended(
1199 true, base::TimeTicks());
1201 // Tell the CrossSiteRequestManager that this RVH has a pending cross-site
1202 // request, so that ResourceDispatcherHost will know to tell us to run the
1203 // old page's unload handler before it sends the response.
1204 // TODO(creis): This needs to be on the RFH.
1205 pending_render_frame_host_
->render_view_host()->
1206 SetHasPendingCrossSiteRequest(true);
1209 // We now have a pending RFH.
1210 DCHECK(!cross_navigation_pending_
);
1211 cross_navigation_pending_
= true;
1213 // Unless we are transferring an existing request, we should now
1214 // tell the old render view to run its beforeunload handler, since it
1215 // doesn't otherwise know that the cross-site request is happening. This
1216 // will trigger a call to ShouldClosePage with the reply.
1218 render_frame_host_
->render_view_host()->FirePageBeforeUnload(true);
1220 return pending_render_frame_host_
;
1223 // Otherwise the same SiteInstance can be used. Navigate render_frame_host_.
1224 DCHECK(!cross_navigation_pending_
);
1225 if (ShouldReuseWebUI(current_entry
, &entry
)) {
1226 pending_web_ui_
.reset();
1227 pending_and_current_web_ui_
= web_ui_
->AsWeakPtr();
1229 SetPendingWebUI(entry
);
1231 // Make sure the new RenderViewHost has the right bindings.
1232 if (pending_web_ui() && !render_frame_host_
->GetProcess()->IsGuest()) {
1233 render_frame_host_
->render_view_host()->AllowBindings(
1234 pending_web_ui()->GetBindings());
1238 if (pending_web_ui() &&
1239 render_frame_host_
->render_view_host()->IsRenderViewLive()) {
1240 pending_web_ui()->GetController()->RenderViewReused(
1241 render_frame_host_
->render_view_host());
1244 // The renderer can exit view source mode when any error or cancellation
1245 // happen. We must overwrite to recover the mode.
1246 if (entry
.IsViewSourceMode()) {
1247 render_frame_host_
->render_view_host()->Send(
1248 new ViewMsg_EnableViewSourceMode(
1249 render_frame_host_
->render_view_host()->GetRoutingID()));
1252 return render_frame_host_
;
1255 void RenderFrameHostManager::CancelPending() {
1256 RenderFrameHostImpl
* pending_render_frame_host
= pending_render_frame_host_
;
1257 pending_render_frame_host_
= NULL
;
1259 RenderViewDevToolsAgentHost::OnCancelPendingNavigation(
1260 pending_render_frame_host
->render_view_host(),
1261 render_frame_host_
->render_view_host());
1263 // We no longer need to prevent the process from exiting.
1264 pending_render_frame_host
->GetProcess()->RemovePendingView();
1266 // The pending RFH may already be on the swapped out list if we started to
1267 // swap it back in and then canceled. If so, make sure it gets swapped out
1268 // again. If it's not on the swapped out list (e.g., aborting a pending
1269 // load), then it's safe to shut down.
1270 if (IsOnSwappedOutList(pending_render_frame_host
)) {
1271 // Any currently suspended navigations are no longer needed.
1272 pending_render_frame_host
->render_view_host()->CancelSuspendedNavigations();
1274 // TODO(creis): We need to swap out the RFH.
1275 pending_render_frame_host
->render_view_host()->SwapOut();
1277 // We won't be coming back, so shut this one down.
1278 delete pending_render_frame_host
;
1281 pending_web_ui_
.reset();
1282 pending_and_current_web_ui_
.reset();
1285 void RenderFrameHostManager::RenderViewDeleted(RenderViewHost
* rvh
) {
1286 // We are doing this in order to work around and to track a crasher
1287 // (http://crbug.com/23411) where it seems that pending_render_frame_host_ is
1288 // deleted (not sure from where) but not NULLed.
1289 if (pending_render_frame_host_
&&
1290 rvh
== pending_render_frame_host_
->render_view_host()) {
1291 // If you hit this NOTREACHED, please report it in the following bug
1292 // http://crbug.com/23411 Make sure to include what you were doing when it
1293 // happened (navigating to a new page, closing a tab...) and if you can
1296 pending_render_frame_host_
= NULL
;
1299 // Make sure deleted RVHs are not kept in the swapped out list while we are
1300 // still alive. (If render_frame_host_ is null, we're already being deleted.)
1301 if (!render_frame_host_
)
1304 // We can't look it up by SiteInstance ID, which may no longer be valid.
1305 for (RenderFrameHostMap::iterator iter
= swapped_out_hosts_
.begin();
1306 iter
!= swapped_out_hosts_
.end();
1308 if (iter
->second
->render_view_host() == rvh
) {
1309 swapped_out_hosts_
.erase(iter
);
1315 bool RenderFrameHostManager::IsRVHOnSwappedOutList(
1316 RenderViewHostImpl
* rvh
) const {
1317 RenderFrameHostImpl
* render_frame_host
= GetSwappedOutRenderFrameHost(
1318 rvh
->GetSiteInstance());
1319 if (!render_frame_host
)
1321 return IsOnSwappedOutList(render_frame_host
);
1324 bool RenderFrameHostManager::IsOnSwappedOutList(
1325 RenderFrameHostImpl
* rfh
) const {
1326 if (!rfh
->render_view_host()->GetSiteInstance())
1329 RenderFrameHostMap::const_iterator iter
= swapped_out_hosts_
.find(
1330 rfh
->render_view_host()->GetSiteInstance()->GetId());
1331 if (iter
== swapped_out_hosts_
.end())
1334 return iter
->second
== rfh
;
1337 RenderViewHostImpl
* RenderFrameHostManager::GetSwappedOutRenderViewHost(
1338 SiteInstance
* instance
) const {
1339 RenderFrameHostImpl
* render_frame_host
=
1340 GetSwappedOutRenderFrameHost(instance
);
1341 if (render_frame_host
)
1342 return render_frame_host
->render_view_host();
1346 RenderFrameHostImpl
* RenderFrameHostManager::GetSwappedOutRenderFrameHost(
1347 SiteInstance
* instance
) const {
1348 RenderFrameHostMap::const_iterator iter
=
1349 swapped_out_hosts_
.find(instance
->GetId());
1350 if (iter
!= swapped_out_hosts_
.end())
1351 return iter
->second
;
1356 } // namespace content