| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "content/browser/frame_host/render_view_host_manager.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 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/renderer_host/render_process_host_impl.h" | |
| 18 #include "content/browser/renderer_host/render_view_host_factory.h" | |
| 19 #include "content/browser/renderer_host/render_view_host_impl.h" | |
| 20 #include "content/browser/site_instance_impl.h" | |
| 21 #include "content/browser/webui/web_ui_controller_factory_registry.h" | |
| 22 #include "content/browser/webui/web_ui_impl.h" | |
| 23 #include "content/common/view_messages.h" | |
| 24 #include "content/port/browser/render_widget_host_view_port.h" | |
| 25 #include "content/public/browser/content_browser_client.h" | |
| 26 #include "content/public/browser/notification_service.h" | |
| 27 #include "content/public/browser/notification_types.h" | |
| 28 #include "content/public/browser/render_widget_host_iterator.h" | |
| 29 #include "content/public/browser/user_metrics.h" | |
| 30 #include "content/public/browser/web_ui_controller.h" | |
| 31 #include "content/public/common/content_switches.h" | |
| 32 #include "content/public/common/url_constants.h" | |
| 33 | |
| 34 namespace content { | |
| 35 | |
| 36 RenderViewHostManager::PendingNavigationParams::PendingNavigationParams() | |
| 37 : is_transfer(false), frame_id(-1), should_replace_current_entry(false) { | |
| 38 } | |
| 39 | |
| 40 RenderViewHostManager::PendingNavigationParams::PendingNavigationParams( | |
| 41 const GlobalRequestID& global_request_id, | |
| 42 bool is_transfer, | |
| 43 const std::vector<GURL>& transfer_url_chain, | |
| 44 Referrer referrer, | |
| 45 PageTransition page_transition, | |
| 46 int64 frame_id, | |
| 47 bool should_replace_current_entry) | |
| 48 : global_request_id(global_request_id), | |
| 49 is_transfer(is_transfer), | |
| 50 transfer_url_chain(transfer_url_chain), | |
| 51 referrer(referrer), | |
| 52 page_transition(page_transition), | |
| 53 frame_id(frame_id), | |
| 54 should_replace_current_entry(should_replace_current_entry) { | |
| 55 } | |
| 56 | |
| 57 RenderViewHostManager::PendingNavigationParams::~PendingNavigationParams() {} | |
| 58 | |
| 59 RenderViewHostManager::RenderViewHostManager( | |
| 60 RenderViewHostDelegate* render_view_delegate, | |
| 61 RenderWidgetHostDelegate* render_widget_delegate, | |
| 62 Delegate* delegate) | |
| 63 : delegate_(delegate), | |
| 64 cross_navigation_pending_(false), | |
| 65 render_view_delegate_(render_view_delegate), | |
| 66 render_widget_delegate_(render_widget_delegate), | |
| 67 render_view_host_(NULL), | |
| 68 pending_render_view_host_(NULL), | |
| 69 interstitial_page_(NULL) { | |
| 70 } | |
| 71 | |
| 72 RenderViewHostManager::~RenderViewHostManager() { | |
| 73 if (pending_render_view_host_) | |
| 74 CancelPending(); | |
| 75 | |
| 76 // We should always have a main RenderViewHost except in some tests. | |
| 77 RenderViewHostImpl* render_view_host = render_view_host_; | |
| 78 render_view_host_ = NULL; | |
| 79 if (render_view_host) | |
| 80 render_view_host->Shutdown(); | |
| 81 | |
| 82 // Shut down any swapped out RenderViewHosts. | |
| 83 for (RenderViewHostMap::iterator iter = swapped_out_hosts_.begin(); | |
| 84 iter != swapped_out_hosts_.end(); | |
| 85 ++iter) { | |
| 86 iter->second->Shutdown(); | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 void RenderViewHostManager::Init(BrowserContext* browser_context, | |
| 91 SiteInstance* site_instance, | |
| 92 int routing_id, | |
| 93 int main_frame_routing_id) { | |
| 94 // Create a RenderViewHost, once we have an instance. It is important to | |
| 95 // immediately give this SiteInstance to a RenderViewHost so that it is | |
| 96 // ref counted. | |
| 97 if (!site_instance) | |
| 98 site_instance = SiteInstance::Create(browser_context); | |
| 99 render_view_host_ = static_cast<RenderViewHostImpl*>( | |
| 100 RenderViewHostFactory::Create( | |
| 101 site_instance, render_view_delegate_, render_widget_delegate_, | |
| 102 routing_id, main_frame_routing_id, false, | |
| 103 delegate_->IsHidden())); | |
| 104 render_view_host_->AttachToFrameTree(); | |
| 105 | |
| 106 // Keep track of renderer processes as they start to shut down or are | |
| 107 // crashed/killed. | |
| 108 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_CLOSED, | |
| 109 NotificationService::AllSources()); | |
| 110 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_CLOSING, | |
| 111 NotificationService::AllSources()); | |
| 112 } | |
| 113 | |
| 114 RenderViewHostImpl* RenderViewHostManager::current_host() const { | |
| 115 return render_view_host_; | |
| 116 } | |
| 117 | |
| 118 RenderViewHostImpl* RenderViewHostManager::pending_render_view_host() const { | |
| 119 return pending_render_view_host_; | |
| 120 } | |
| 121 | |
| 122 RenderWidgetHostView* RenderViewHostManager::GetRenderWidgetHostView() const { | |
| 123 if (interstitial_page_) | |
| 124 return interstitial_page_->GetView(); | |
| 125 if (!render_view_host_) | |
| 126 return NULL; | |
| 127 return render_view_host_->GetView(); | |
| 128 } | |
| 129 | |
| 130 void RenderViewHostManager::SetPendingWebUI(const NavigationEntryImpl& entry) { | |
| 131 pending_web_ui_.reset( | |
| 132 delegate_->CreateWebUIForRenderManager(entry.GetURL())); | |
| 133 pending_and_current_web_ui_.reset(); | |
| 134 | |
| 135 // If we have assigned (zero or more) bindings to this NavigationEntry in the | |
| 136 // past, make sure we're not granting it different bindings than it had | |
| 137 // before. If so, note it and don't give it any bindings, to avoid a | |
| 138 // potential privilege escalation. | |
| 139 if (pending_web_ui_.get() && | |
| 140 entry.bindings() != NavigationEntryImpl::kInvalidBindings && | |
| 141 pending_web_ui_->GetBindings() != entry.bindings()) { | |
| 142 RecordAction(UserMetricsAction("ProcessSwapBindingsMismatch_RVHM")); | |
| 143 pending_web_ui_.reset(); | |
| 144 } | |
| 145 } | |
| 146 | |
| 147 RenderViewHostImpl* RenderViewHostManager::Navigate( | |
| 148 const NavigationEntryImpl& entry) { | |
| 149 TRACE_EVENT0("browser", "RenderViewHostManager:Navigate"); | |
| 150 // Create a pending RenderViewHost. It will give us the one we should use | |
| 151 RenderViewHostImpl* dest_render_view_host = | |
| 152 static_cast<RenderViewHostImpl*>(UpdateRendererStateForNavigate(entry)); | |
| 153 if (!dest_render_view_host) | |
| 154 return NULL; // We weren't able to create a pending render view host. | |
| 155 | |
| 156 // If the current render_view_host_ isn't live, we should create it so | |
| 157 // that we don't show a sad tab while the dest_render_view_host fetches | |
| 158 // its first page. (Bug 1145340) | |
| 159 if (dest_render_view_host != render_view_host_ && | |
| 160 !render_view_host_->IsRenderViewLive()) { | |
| 161 // Note: we don't call InitRenderView here because we are navigating away | |
| 162 // soon anyway, and we don't have the NavigationEntry for this host. | |
| 163 delegate_->CreateRenderViewForRenderManager(render_view_host_, | |
| 164 MSG_ROUTING_NONE); | |
| 165 } | |
| 166 | |
| 167 // If the renderer crashed, then try to create a new one to satisfy this | |
| 168 // navigation request. | |
| 169 if (!dest_render_view_host->IsRenderViewLive()) { | |
| 170 // Recreate the opener chain. | |
| 171 int opener_route_id = delegate_->CreateOpenerRenderViewsForRenderManager( | |
| 172 dest_render_view_host->GetSiteInstance()); | |
| 173 if (!InitRenderView(dest_render_view_host, opener_route_id)) | |
| 174 return NULL; | |
| 175 | |
| 176 // Now that we've created a new renderer, be sure to hide it if it isn't | |
| 177 // our primary one. Otherwise, we might crash if we try to call Show() | |
| 178 // on it later. | |
| 179 if (dest_render_view_host != render_view_host_ && | |
| 180 dest_render_view_host->GetView()) { | |
| 181 dest_render_view_host->GetView()->Hide(); | |
| 182 } else { | |
| 183 // This is our primary renderer, notify here as we won't be calling | |
| 184 // CommitPending (which does the notify). | |
| 185 delegate_->NotifySwappedFromRenderManager(NULL, render_view_host_); | |
| 186 } | |
| 187 } | |
| 188 | |
| 189 return dest_render_view_host; | |
| 190 } | |
| 191 | |
| 192 void RenderViewHostManager::Stop() { | |
| 193 render_view_host_->Stop(); | |
| 194 | |
| 195 // If we are cross-navigating, we should stop the pending renderers. This | |
| 196 // will lead to a DidFailProvisionalLoad, which will properly destroy them. | |
| 197 if (cross_navigation_pending_) { | |
| 198 pending_render_view_host_->Send( | |
| 199 new ViewMsg_Stop(pending_render_view_host_->GetRoutingID())); | |
| 200 } | |
| 201 } | |
| 202 | |
| 203 void RenderViewHostManager::SetIsLoading(bool is_loading) { | |
| 204 render_view_host_->SetIsLoading(is_loading); | |
| 205 if (pending_render_view_host_) | |
| 206 pending_render_view_host_->SetIsLoading(is_loading); | |
| 207 } | |
| 208 | |
| 209 bool RenderViewHostManager::ShouldCloseTabOnUnresponsiveRenderer() { | |
| 210 if (!cross_navigation_pending_) | |
| 211 return true; | |
| 212 | |
| 213 // We should always have a pending RVH when there's a cross-process navigation | |
| 214 // in progress. Sanity check this for http://crbug.com/276333. | |
| 215 CHECK(pending_render_view_host_); | |
| 216 | |
| 217 // If the tab becomes unresponsive during {before}unload while doing a | |
| 218 // cross-site navigation, proceed with the navigation. (This assumes that | |
| 219 // the pending RenderViewHost is still responsive.) | |
| 220 if (render_view_host_->is_waiting_for_unload_ack()) { | |
| 221 // The request has been started and paused while we're waiting for the | |
| 222 // unload handler to finish. We'll pretend that it did. The pending | |
| 223 // renderer will then be swapped in as part of the usual DidNavigate logic. | |
| 224 // (If the unload handler later finishes, this call will be ignored because | |
| 225 // the pending_nav_params_ state will already be cleaned up.) | |
| 226 current_host()->OnSwappedOut(true); | |
| 227 } else if (render_view_host_->is_waiting_for_beforeunload_ack()) { | |
| 228 // Haven't gotten around to starting the request, because we're still | |
| 229 // waiting for the beforeunload handler to finish. We'll pretend that it | |
| 230 // did finish, to let the navigation proceed. Note that there's a danger | |
| 231 // that the beforeunload handler will later finish and possibly return | |
| 232 // false (meaning the navigation should not proceed), but we'll ignore it | |
| 233 // in this case because it took too long. | |
| 234 if (pending_render_view_host_->are_navigations_suspended()) | |
| 235 pending_render_view_host_->SetNavigationsSuspended( | |
| 236 false, base::TimeTicks::Now()); | |
| 237 } | |
| 238 return false; | |
| 239 } | |
| 240 | |
| 241 void RenderViewHostManager::SwappedOut(RenderViewHost* render_view_host) { | |
| 242 // Make sure this is from our current RVH, and that we have a pending | |
| 243 // navigation from OnCrossSiteResponse. (There may be no pending navigation | |
| 244 // for data URLs that don't make network requests, for example.) If not, | |
| 245 // just return early and ignore. | |
| 246 if (render_view_host != render_view_host_ || !pending_nav_params_.get()) { | |
| 247 pending_nav_params_.reset(); | |
| 248 return; | |
| 249 } | |
| 250 | |
| 251 // Now that the unload handler has run, we need to either initiate the | |
| 252 // pending transfer (if there is one) or resume the paused response (if not). | |
| 253 // TODO(creis): The blank swapped out page is visible during this time, but | |
| 254 // we can shorten this by delivering the response directly, rather than | |
| 255 // forcing an identical request to be made. | |
| 256 if (pending_nav_params_->is_transfer) { | |
| 257 // Treat the last URL in the chain as the destination and the remainder as | |
| 258 // the redirect chain. | |
| 259 CHECK(pending_nav_params_->transfer_url_chain.size()); | |
| 260 GURL transfer_url = pending_nav_params_->transfer_url_chain.back(); | |
| 261 pending_nav_params_->transfer_url_chain.pop_back(); | |
| 262 | |
| 263 // We don't know whether the original request had |user_action| set to true. | |
| 264 // However, since we force the navigation to be in the current tab, it | |
| 265 // doesn't matter. | |
| 266 render_view_host->GetDelegate()->RequestTransferURL( | |
| 267 transfer_url, | |
| 268 pending_nav_params_->transfer_url_chain, | |
| 269 pending_nav_params_->referrer, | |
| 270 pending_nav_params_->page_transition, | |
| 271 CURRENT_TAB, | |
| 272 pending_nav_params_->frame_id, | |
| 273 pending_nav_params_->global_request_id, | |
| 274 pending_nav_params_->should_replace_current_entry, | |
| 275 true); | |
| 276 } else if (pending_render_view_host_) { | |
| 277 RenderProcessHostImpl* pending_process = | |
| 278 static_cast<RenderProcessHostImpl*>( | |
| 279 pending_render_view_host_->GetProcess()); | |
| 280 pending_process->ResumeDeferredNavigation( | |
| 281 pending_nav_params_->global_request_id); | |
| 282 } | |
| 283 pending_nav_params_.reset(); | |
| 284 } | |
| 285 | |
| 286 void RenderViewHostManager::DidNavigateMainFrame( | |
| 287 RenderViewHost* render_view_host) { | |
| 288 if (!cross_navigation_pending_) { | |
| 289 DCHECK(!pending_render_view_host_); | |
| 290 | |
| 291 // We should only hear this from our current renderer. | |
| 292 DCHECK(render_view_host == render_view_host_); | |
| 293 | |
| 294 // Even when there is no pending RVH, there may be a pending Web UI. | |
| 295 if (pending_web_ui()) | |
| 296 CommitPending(); | |
| 297 return; | |
| 298 } | |
| 299 | |
| 300 if (render_view_host == pending_render_view_host_) { | |
| 301 // The pending cross-site navigation completed, so show the renderer. | |
| 302 // If it committed without sending network requests (e.g., data URLs), | |
| 303 // then we still need to swap out the old RVH first and run its unload | |
| 304 // handler. OK for that to happen in the background. | |
| 305 if (pending_render_view_host_->HasPendingCrossSiteRequest()) | |
| 306 SwapOutOldPage(); | |
| 307 | |
| 308 CommitPending(); | |
| 309 cross_navigation_pending_ = false; | |
| 310 } else if (render_view_host == render_view_host_) { | |
| 311 // A navigation in the original page has taken place. Cancel the pending | |
| 312 // one. | |
| 313 CancelPending(); | |
| 314 cross_navigation_pending_ = false; | |
| 315 } else { | |
| 316 // No one else should be sending us DidNavigate in this state. | |
| 317 DCHECK(false); | |
| 318 } | |
| 319 } | |
| 320 | |
| 321 void RenderViewHostManager::DidDisownOpener(RenderViewHost* render_view_host) { | |
| 322 // Notify all swapped out hosts, including the pending RVH. | |
| 323 for (RenderViewHostMap::iterator iter = swapped_out_hosts_.begin(); | |
| 324 iter != swapped_out_hosts_.end(); | |
| 325 ++iter) { | |
| 326 DCHECK_NE(iter->second->GetSiteInstance(), | |
| 327 current_host()->GetSiteInstance()); | |
| 328 iter->second->DisownOpener(); | |
| 329 } | |
| 330 } | |
| 331 | |
| 332 void RenderViewHostManager::RendererAbortedProvisionalLoad( | |
| 333 RenderViewHost* render_view_host) { | |
| 334 // We used to cancel the pending renderer here for cross-site downloads. | |
| 335 // However, it's not safe to do that because the download logic repeatedly | |
| 336 // looks for this WebContents based on a render view ID. Instead, we just | |
| 337 // leave the pending renderer around until the next navigation event | |
| 338 // (Navigate, DidNavigate, etc), which will clean it up properly. | |
| 339 // TODO(creis): All of this will go away when we move the cross-site logic | |
| 340 // to ResourceDispatcherHost, so that we intercept responses rather than | |
| 341 // navigation events. (That's necessary to support onunload anyway.) Once | |
| 342 // we've made that change, we won't create a pending renderer until we know | |
| 343 // the response is not a download. | |
| 344 } | |
| 345 | |
| 346 void RenderViewHostManager::RendererProcessClosing( | |
| 347 RenderProcessHost* render_process_host) { | |
| 348 // Remove any swapped out RVHs from this process, so that we don't try to | |
| 349 // swap them back in while the process is exiting. Start by finding them, | |
| 350 // since there could be more than one. | |
| 351 std::list<int> ids_to_remove; | |
| 352 for (RenderViewHostMap::iterator iter = swapped_out_hosts_.begin(); | |
| 353 iter != swapped_out_hosts_.end(); | |
| 354 ++iter) { | |
| 355 if (iter->second->GetProcess() == render_process_host) | |
| 356 ids_to_remove.push_back(iter->first); | |
| 357 } | |
| 358 | |
| 359 // Now delete them. | |
| 360 while (!ids_to_remove.empty()) { | |
| 361 swapped_out_hosts_[ids_to_remove.back()]->Shutdown(); | |
| 362 swapped_out_hosts_.erase(ids_to_remove.back()); | |
| 363 ids_to_remove.pop_back(); | |
| 364 } | |
| 365 } | |
| 366 | |
| 367 void RenderViewHostManager::ShouldClosePage( | |
| 368 bool for_cross_site_transition, | |
| 369 bool proceed, | |
| 370 const base::TimeTicks& proceed_time) { | |
| 371 if (for_cross_site_transition) { | |
| 372 // Ignore if we're not in a cross-site navigation. | |
| 373 if (!cross_navigation_pending_) | |
| 374 return; | |
| 375 | |
| 376 if (proceed) { | |
| 377 // Ok to unload the current page, so proceed with the cross-site | |
| 378 // navigation. Note that if navigations are not currently suspended, it | |
| 379 // might be because the renderer was deemed unresponsive and this call was | |
| 380 // already made by ShouldCloseTabOnUnresponsiveRenderer. In that case, it | |
| 381 // is ok to do nothing here. | |
| 382 if (pending_render_view_host_ && | |
| 383 pending_render_view_host_->are_navigations_suspended()) { | |
| 384 pending_render_view_host_->SetNavigationsSuspended(false, proceed_time); | |
| 385 } | |
| 386 } else { | |
| 387 // Current page says to cancel. | |
| 388 CancelPending(); | |
| 389 cross_navigation_pending_ = false; | |
| 390 } | |
| 391 } else { | |
| 392 // Non-cross site transition means closing the entire tab. | |
| 393 bool proceed_to_fire_unload; | |
| 394 delegate_->BeforeUnloadFiredFromRenderManager(proceed, proceed_time, | |
| 395 &proceed_to_fire_unload); | |
| 396 | |
| 397 if (proceed_to_fire_unload) { | |
| 398 // If we're about to close the tab and there's a pending RVH, cancel it. | |
| 399 // Otherwise, if the navigation in the pending RVH completes before the | |
| 400 // close in the current RVH, we'll lose the tab close. | |
| 401 if (pending_render_view_host_) { | |
| 402 CancelPending(); | |
| 403 cross_navigation_pending_ = false; | |
| 404 } | |
| 405 | |
| 406 // This is not a cross-site navigation, the tab is being closed. | |
| 407 render_view_host_->ClosePage(); | |
| 408 } | |
| 409 } | |
| 410 } | |
| 411 | |
| 412 void RenderViewHostManager::OnCrossSiteResponse( | |
| 413 RenderViewHost* pending_render_view_host, | |
| 414 const GlobalRequestID& global_request_id, | |
| 415 bool is_transfer, | |
| 416 const std::vector<GURL>& transfer_url_chain, | |
| 417 const Referrer& referrer, | |
| 418 PageTransition page_transition, | |
| 419 int64 frame_id, | |
| 420 bool should_replace_current_entry) { | |
| 421 // This should be called either when the pending RVH is ready to commit or | |
| 422 // when we realize that the current RVH's request requires a transfer. | |
| 423 DCHECK( | |
| 424 pending_render_view_host == pending_render_view_host_ || | |
| 425 pending_render_view_host == render_view_host_); | |
| 426 | |
| 427 // TODO(creis): Eventually we will want to check all navigation responses | |
| 428 // here, but currently we pass information for a transfer if | |
| 429 // ShouldSwapProcessesForRedirect returned true in the network stack. | |
| 430 // In that case, we should set up a transfer after the unload handler runs. | |
| 431 // If is_transfer is false, we will just run the unload handler and resume. | |
| 432 pending_nav_params_.reset(new PendingNavigationParams( | |
| 433 global_request_id, is_transfer, transfer_url_chain, referrer, | |
| 434 page_transition, frame_id, should_replace_current_entry)); | |
| 435 | |
| 436 // Run the unload handler of the current page. | |
| 437 SwapOutOldPage(); | |
| 438 } | |
| 439 | |
| 440 void RenderViewHostManager::SwapOutOldPage() { | |
| 441 // Should only see this while we have a pending renderer or transfer. | |
| 442 CHECK(cross_navigation_pending_ || pending_nav_params_.get()); | |
| 443 | |
| 444 // Tell the renderer to suppress any further modal dialogs so that we can swap | |
| 445 // it out. This must be done before canceling any current dialog, in case | |
| 446 // there is a loop creating additional dialogs. | |
| 447 render_view_host_->SuppressDialogsUntilSwapOut(); | |
| 448 | |
| 449 // Now close any modal dialogs that would prevent us from swapping out. This | |
| 450 // must be done separately from SwapOut, so that the PageGroupLoadDeferrer is | |
| 451 // no longer on the stack when we send the SwapOut message. | |
| 452 delegate_->CancelModalDialogsForRenderManager(); | |
| 453 | |
| 454 // Tell the old renderer it is being swapped out. This will fire the unload | |
| 455 // handler (without firing the beforeunload handler a second time). When the | |
| 456 // unload handler finishes and the navigation completes, we will send a | |
| 457 // message to the ResourceDispatcherHost, allowing the pending RVH's response | |
| 458 // to resume. | |
| 459 render_view_host_->SwapOut(); | |
| 460 | |
| 461 // ResourceDispatcherHost has told us to run the onunload handler, which | |
| 462 // means it is not a download or unsafe page, and we are going to perform the | |
| 463 // navigation. Thus, we no longer need to remember that the RenderViewHost | |
| 464 // is part of a pending cross-site request. | |
| 465 if (pending_render_view_host_) | |
| 466 pending_render_view_host_->SetHasPendingCrossSiteRequest(false); | |
| 467 } | |
| 468 | |
| 469 void RenderViewHostManager::Observe( | |
| 470 int type, | |
| 471 const NotificationSource& source, | |
| 472 const NotificationDetails& details) { | |
| 473 switch (type) { | |
| 474 case NOTIFICATION_RENDERER_PROCESS_CLOSED: | |
| 475 case NOTIFICATION_RENDERER_PROCESS_CLOSING: | |
| 476 RendererProcessClosing( | |
| 477 Source<RenderProcessHost>(source).ptr()); | |
| 478 break; | |
| 479 | |
| 480 default: | |
| 481 NOTREACHED(); | |
| 482 } | |
| 483 } | |
| 484 | |
| 485 bool RenderViewHostManager::ShouldTransitionCrossSite() { | |
| 486 // False in the single-process mode, as it makes RVHs to accumulate | |
| 487 // in swapped_out_hosts_. | |
| 488 // True if we are using process-per-site-instance (default) or | |
| 489 // process-per-site (kProcessPerSite). | |
| 490 return | |
| 491 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess) && | |
| 492 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kProcessPerTab); | |
| 493 } | |
| 494 | |
| 495 bool RenderViewHostManager::ShouldSwapBrowsingInstancesForNavigation( | |
| 496 const NavigationEntry* current_entry, | |
| 497 const NavigationEntryImpl* new_entry) const { | |
| 498 DCHECK(new_entry); | |
| 499 | |
| 500 // If new_entry already has a SiteInstance, assume it is correct and use it. | |
| 501 if (new_entry->site_instance()) | |
| 502 return false; | |
| 503 | |
| 504 // Check for reasons to swap processes even if we are in a process model that | |
| 505 // doesn't usually swap (e.g., process-per-tab). Any time we return true, | |
| 506 // the new_entry will be rendered in a new SiteInstance AND BrowsingInstance. | |
| 507 | |
| 508 // We use the effective URL here, since that's what is used in the | |
| 509 // SiteInstance's site and when we later call IsSameWebSite. If there is no | |
| 510 // current_entry, check the current SiteInstance's site, which might already | |
| 511 // be committed to a Web UI URL (such as the NTP). | |
| 512 BrowserContext* browser_context = | |
| 513 delegate_->GetControllerForRenderManager().GetBrowserContext(); | |
| 514 const GURL& current_url = (current_entry) ? | |
| 515 SiteInstanceImpl::GetEffectiveURL(browser_context, | |
| 516 current_entry->GetURL()) : | |
| 517 render_view_host_->GetSiteInstance()->GetSiteURL(); | |
| 518 const GURL& new_url = SiteInstanceImpl::GetEffectiveURL(browser_context, | |
| 519 new_entry->GetURL()); | |
| 520 | |
| 521 // For security, we should transition between processes when one is a Web UI | |
| 522 // page and one isn't. | |
| 523 if (WebUIControllerFactoryRegistry::GetInstance()->UseWebUIForURL( | |
| 524 browser_context, current_url)) { | |
| 525 // If so, force a swap if destination is not an acceptable URL for Web UI. | |
| 526 // Here, data URLs are never allowed. | |
| 527 if (!WebUIControllerFactoryRegistry::GetInstance()->IsURLAcceptableForWebUI( | |
| 528 browser_context, new_url, false)) { | |
| 529 return true; | |
| 530 } | |
| 531 } else { | |
| 532 // Force a swap if it's a Web UI URL. | |
| 533 if (WebUIControllerFactoryRegistry::GetInstance()->UseWebUIForURL( | |
| 534 browser_context, new_url)) { | |
| 535 return true; | |
| 536 } | |
| 537 } | |
| 538 | |
| 539 // Check with the content client as well. Important to pass current_url here, | |
| 540 // which uses the SiteInstance's site if there is no current_entry. | |
| 541 if (GetContentClient()->browser()->ShouldSwapBrowsingInstancesForNavigation( | |
| 542 render_view_host_->GetSiteInstance(), current_url, new_url)) { | |
| 543 return true; | |
| 544 } | |
| 545 | |
| 546 // We can't switch a RenderView between view source and non-view source mode | |
| 547 // without screwing up the session history sometimes (when navigating between | |
| 548 // "view-source:http://foo.com/" and "http://foo.com/", Blink doesn't treat | |
| 549 // it as a new navigation). So require a BrowsingInstance switch. | |
| 550 if (current_entry && | |
| 551 current_entry->IsViewSourceMode() != new_entry->IsViewSourceMode()) | |
| 552 return true; | |
| 553 | |
| 554 return false; | |
| 555 } | |
| 556 | |
| 557 bool RenderViewHostManager::ShouldReuseWebUI( | |
| 558 const NavigationEntry* current_entry, | |
| 559 const NavigationEntryImpl* new_entry) const { | |
| 560 NavigationControllerImpl& controller = | |
| 561 delegate_->GetControllerForRenderManager(); | |
| 562 return current_entry && web_ui_.get() && | |
| 563 (WebUIControllerFactoryRegistry::GetInstance()->GetWebUIType( | |
| 564 controller.GetBrowserContext(), current_entry->GetURL()) == | |
| 565 WebUIControllerFactoryRegistry::GetInstance()->GetWebUIType( | |
| 566 controller.GetBrowserContext(), new_entry->GetURL())); | |
| 567 } | |
| 568 | |
| 569 SiteInstance* RenderViewHostManager::GetSiteInstanceForEntry( | |
| 570 const NavigationEntryImpl& entry, | |
| 571 SiteInstance* current_instance, | |
| 572 bool force_browsing_instance_swap) { | |
| 573 // Determine which SiteInstance to use for navigating to |entry|. | |
| 574 const GURL& dest_url = entry.GetURL(); | |
| 575 NavigationControllerImpl& controller = | |
| 576 delegate_->GetControllerForRenderManager(); | |
| 577 BrowserContext* browser_context = controller.GetBrowserContext(); | |
| 578 | |
| 579 // If a swap is required, we need to force the SiteInstance AND | |
| 580 // BrowsingInstance to be different ones, using CreateForURL. | |
| 581 if (force_browsing_instance_swap) { | |
| 582 // We shouldn't be forcing a swap if an entry already has a SiteInstance. | |
| 583 CHECK(!entry.site_instance()); | |
| 584 return SiteInstance::CreateForURL(browser_context, dest_url); | |
| 585 } | |
| 586 | |
| 587 // If the entry has an instance already we should use it. | |
| 588 if (entry.site_instance()) | |
| 589 return entry.site_instance(); | |
| 590 | |
| 591 // (UGLY) HEURISTIC, process-per-site only: | |
| 592 // | |
| 593 // If this navigation is generated, then it probably corresponds to a search | |
| 594 // query. Given that search results typically lead to users navigating to | |
| 595 // other sites, we don't really want to use the search engine hostname to | |
| 596 // determine the site instance for this navigation. | |
| 597 // | |
| 598 // NOTE: This can be removed once we have a way to transition between | |
| 599 // RenderViews in response to a link click. | |
| 600 // | |
| 601 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kProcessPerSite) && | |
| 602 PageTransitionCoreTypeIs(entry.GetTransitionType(), | |
| 603 PAGE_TRANSITION_GENERATED)) { | |
| 604 return current_instance; | |
| 605 } | |
| 606 | |
| 607 SiteInstanceImpl* current_site_instance = | |
| 608 static_cast<SiteInstanceImpl*>(current_instance); | |
| 609 | |
| 610 // If we haven't used our SiteInstance (and thus RVH) yet, then we can use it | |
| 611 // for this entry. We won't commit the SiteInstance to this site until the | |
| 612 // navigation commits (in DidNavigate), unless the navigation entry was | |
| 613 // restored or it's a Web UI as described below. | |
| 614 if (!current_site_instance->HasSite()) { | |
| 615 // If we've already created a SiteInstance for our destination, we don't | |
| 616 // want to use this unused SiteInstance; use the existing one. (We don't | |
| 617 // do this check if the current_instance has a site, because for now, we | |
| 618 // want to compare against the current URL and not the SiteInstance's site. | |
| 619 // In this case, there is no current URL, so comparing against the site is | |
| 620 // ok. See additional comments below.) | |
| 621 // | |
| 622 // Also, if the URL should use process-per-site mode and there is an | |
| 623 // existing process for the site, we should use it. We can call | |
| 624 // GetRelatedSiteInstance() for this, which will eagerly set the site and | |
| 625 // thus use the correct process. | |
| 626 bool use_process_per_site = | |
| 627 RenderProcessHost::ShouldUseProcessPerSite(browser_context, dest_url) && | |
| 628 RenderProcessHostImpl::GetProcessHostForSite(browser_context, dest_url); | |
| 629 if (current_site_instance->HasRelatedSiteInstance(dest_url) || | |
| 630 use_process_per_site) { | |
| 631 return current_site_instance->GetRelatedSiteInstance(dest_url); | |
| 632 } | |
| 633 | |
| 634 // For extensions, Web UI URLs (such as the new tab page), and apps we do | |
| 635 // not want to use the current_instance if it has no site, since it will | |
| 636 // have a RenderProcessHost of PRIV_NORMAL. Create a new SiteInstance for | |
| 637 // this URL instead (with the correct process type). | |
| 638 if (current_site_instance->HasWrongProcessForURL(dest_url)) | |
| 639 return current_site_instance->GetRelatedSiteInstance(dest_url); | |
| 640 | |
| 641 // View-source URLs must use a new SiteInstance and BrowsingInstance. | |
| 642 // TODO(nasko): This is the same condition as later in the function. This | |
| 643 // should be taken into account when refactoring this method as part of | |
| 644 // http://crbug.com/123007. | |
| 645 if (entry.IsViewSourceMode()) | |
| 646 return SiteInstance::CreateForURL(browser_context, dest_url); | |
| 647 | |
| 648 // If we are navigating from a blank SiteInstance to a WebUI, make sure we | |
| 649 // create a new SiteInstance. | |
| 650 if (WebUIControllerFactoryRegistry::GetInstance()->UseWebUIForURL( | |
| 651 browser_context, dest_url)) { | |
| 652 return SiteInstance::CreateForURL(browser_context, dest_url); | |
| 653 } | |
| 654 | |
| 655 // Normally the "site" on the SiteInstance is set lazily when the load | |
| 656 // actually commits. This is to support better process sharing in case | |
| 657 // the site redirects to some other site: we want to use the destination | |
| 658 // site in the site instance. | |
| 659 // | |
| 660 // In the case of session restore, as it loads all the pages immediately | |
| 661 // we need to set the site first, otherwise after a restore none of the | |
| 662 // pages would share renderers in process-per-site. | |
| 663 if (entry.restore_type() != NavigationEntryImpl::RESTORE_NONE) | |
| 664 current_site_instance->SetSite(dest_url); | |
| 665 | |
| 666 return current_site_instance; | |
| 667 } | |
| 668 | |
| 669 // Otherwise, only create a new SiteInstance for a cross-site navigation. | |
| 670 | |
| 671 // TODO(creis): Once we intercept links and script-based navigations, we | |
| 672 // will be able to enforce that all entries in a SiteInstance actually have | |
| 673 // the same site, and it will be safe to compare the URL against the | |
| 674 // SiteInstance's site, as follows: | |
| 675 // const GURL& current_url = current_instance->site(); | |
| 676 // For now, though, we're in a hybrid model where you only switch | |
| 677 // SiteInstances if you type in a cross-site URL. This means we have to | |
| 678 // compare the entry's URL to the last committed entry's URL. | |
| 679 NavigationEntry* current_entry = controller.GetLastCommittedEntry(); | |
| 680 if (interstitial_page_) { | |
| 681 // The interstitial is currently the last committed entry, but we want to | |
| 682 // compare against the last non-interstitial entry. | |
| 683 current_entry = controller.GetEntryAtOffset(-1); | |
| 684 } | |
| 685 // If there is no last non-interstitial entry (and current_instance already | |
| 686 // has a site), then we must have been opened from another tab. We want | |
| 687 // to compare against the URL of the page that opened us, but we can't | |
| 688 // get to it directly. The best we can do is check against the site of | |
| 689 // the SiteInstance. This will be correct when we intercept links and | |
| 690 // script-based navigations, but for now, it could place some pages in a | |
| 691 // new process unnecessarily. We should only hit this case if a page tries | |
| 692 // to open a new tab to an interstitial-inducing URL, and then navigates | |
| 693 // the page to a different same-site URL. (This seems very unlikely in | |
| 694 // practice.) | |
| 695 const GURL& current_url = (current_entry) ? current_entry->GetURL() : | |
| 696 current_instance->GetSiteURL(); | |
| 697 | |
| 698 // View-source URLs must use a new SiteInstance and BrowsingInstance. | |
| 699 // TODO(creis): Refactor this method so this duplicated code isn't needed. | |
| 700 // See http://crbug.com/123007. | |
| 701 if (current_entry && | |
| 702 current_entry->IsViewSourceMode() != entry.IsViewSourceMode()) { | |
| 703 return SiteInstance::CreateForURL(browser_context, dest_url); | |
| 704 } | |
| 705 | |
| 706 // Use the current SiteInstance for same site navigations, as long as the | |
| 707 // process type is correct. (The URL may have been installed as an app since | |
| 708 // the last time we visited it.) | |
| 709 if (SiteInstance::IsSameWebSite(browser_context, current_url, dest_url) && | |
| 710 !current_site_instance->HasWrongProcessForURL(dest_url)) { | |
| 711 return current_instance; | |
| 712 } | |
| 713 | |
| 714 // Start the new renderer in a new SiteInstance, but in the current | |
| 715 // BrowsingInstance. It is important to immediately give this new | |
| 716 // SiteInstance to a RenderViewHost (if it is different than our current | |
| 717 // SiteInstance), so that it is ref counted. This will happen in | |
| 718 // CreateRenderView. | |
| 719 return current_instance->GetRelatedSiteInstance(dest_url); | |
| 720 } | |
| 721 | |
| 722 int RenderViewHostManager::CreateRenderView( | |
| 723 SiteInstance* instance, | |
| 724 int opener_route_id, | |
| 725 bool swapped_out, | |
| 726 bool hidden) { | |
| 727 CHECK(instance); | |
| 728 DCHECK(!swapped_out || hidden); // Swapped out views should always be hidden. | |
| 729 | |
| 730 // We are creating a pending or swapped out RVH here. We should never create | |
| 731 // it in the same SiteInstance as our current RVH. | |
| 732 CHECK_NE(render_view_host_->GetSiteInstance(), instance); | |
| 733 | |
| 734 // Check if we've already created an RVH for this SiteInstance. If so, try | |
| 735 // to re-use the existing one, which has already been initialized. We'll | |
| 736 // remove it from the list of swapped out hosts if it commits. | |
| 737 RenderViewHostImpl* new_render_view_host = static_cast<RenderViewHostImpl*>( | |
| 738 GetSwappedOutRenderViewHost(instance)); | |
| 739 if (new_render_view_host) { | |
| 740 // Prevent the process from exiting while we're trying to use it. | |
| 741 if (!swapped_out) | |
| 742 new_render_view_host->GetProcess()->AddPendingView(); | |
| 743 } else { | |
| 744 // Create a new RenderViewHost if we don't find an existing one. | |
| 745 new_render_view_host = static_cast<RenderViewHostImpl*>( | |
| 746 RenderViewHostFactory::Create(instance, | |
| 747 render_view_delegate_, | |
| 748 render_widget_delegate_, | |
| 749 MSG_ROUTING_NONE, | |
| 750 MSG_ROUTING_NONE, | |
| 751 swapped_out, | |
| 752 hidden)); | |
| 753 | |
| 754 // If the new RVH is swapped out already, store it. Otherwise prevent the | |
| 755 // process from exiting while we're trying to navigate in it. | |
| 756 if (swapped_out) { | |
| 757 swapped_out_hosts_[instance->GetId()] = new_render_view_host; | |
| 758 } else { | |
| 759 new_render_view_host->GetProcess()->AddPendingView(); | |
| 760 } | |
| 761 | |
| 762 bool success = InitRenderView(new_render_view_host, opener_route_id); | |
| 763 if (success) { | |
| 764 // Don't show the view until we get a DidNavigate from it. | |
| 765 new_render_view_host->GetView()->Hide(); | |
| 766 } else if (!swapped_out) { | |
| 767 CancelPending(); | |
| 768 } | |
| 769 } | |
| 770 | |
| 771 // Use this as our new pending RVH if it isn't swapped out. | |
| 772 if (!swapped_out) | |
| 773 pending_render_view_host_ = new_render_view_host; | |
| 774 | |
| 775 return new_render_view_host->GetRoutingID(); | |
| 776 } | |
| 777 | |
| 778 bool RenderViewHostManager::InitRenderView(RenderViewHost* render_view_host, | |
| 779 int opener_route_id) { | |
| 780 // If the pending navigation is to a WebUI and the RenderView is not in a | |
| 781 // guest process, tell the RenderView about any bindings it will need enabled. | |
| 782 if (pending_web_ui() && !render_view_host->GetProcess()->IsGuest()) { | |
| 783 render_view_host->AllowBindings(pending_web_ui()->GetBindings()); | |
| 784 } else { | |
| 785 // Ensure that we don't create an unprivileged RenderView in a WebUI-enabled | |
| 786 // process. | |
| 787 CHECK(!ChildProcessSecurityPolicyImpl::GetInstance()->HasWebUIBindings( | |
| 788 render_view_host->GetProcess()->GetID())); | |
| 789 } | |
| 790 | |
| 791 return delegate_->CreateRenderViewForRenderManager(render_view_host, | |
| 792 opener_route_id); | |
| 793 } | |
| 794 | |
| 795 void RenderViewHostManager::CommitPending() { | |
| 796 // First check whether we're going to want to focus the location bar after | |
| 797 // this commit. We do this now because the navigation hasn't formally | |
| 798 // committed yet, so if we've already cleared |pending_web_ui_| the call chain | |
| 799 // this triggers won't be able to figure out what's going on. | |
| 800 bool will_focus_location_bar = delegate_->FocusLocationBarByDefault(); | |
| 801 | |
| 802 // Next commit the Web UI, if any. Either replace |web_ui_| with | |
| 803 // |pending_web_ui_|, or clear |web_ui_| if there is no pending WebUI, or | |
| 804 // leave |web_ui_| as is if reusing it. | |
| 805 DCHECK(!(pending_web_ui_.get() && pending_and_current_web_ui_.get())); | |
| 806 if (pending_web_ui_) | |
| 807 web_ui_.reset(pending_web_ui_.release()); | |
| 808 else if (!pending_and_current_web_ui_.get()) | |
| 809 web_ui_.reset(); | |
| 810 | |
| 811 // It's possible for the pending_render_view_host_ to be NULL when we aren't | |
| 812 // crossing process boundaries. If so, we just needed to handle the Web UI | |
| 813 // committing above and we're done. | |
| 814 if (!pending_render_view_host_) { | |
| 815 if (will_focus_location_bar) | |
| 816 delegate_->SetFocusToLocationBar(false); | |
| 817 return; | |
| 818 } | |
| 819 | |
| 820 // Remember if the page was focused so we can focus the new renderer in | |
| 821 // that case. | |
| 822 bool focus_render_view = !will_focus_location_bar && | |
| 823 render_view_host_->GetView() && render_view_host_->GetView()->HasFocus(); | |
| 824 | |
| 825 // Swap in the pending view and make it active. Also ensure the FrameTree | |
| 826 // stays in sync. | |
| 827 RenderViewHostImpl* old_render_view_host = render_view_host_; | |
| 828 render_view_host_ = pending_render_view_host_; | |
| 829 pending_render_view_host_ = NULL; | |
| 830 render_view_host_->AttachToFrameTree(); | |
| 831 | |
| 832 // The process will no longer try to exit, so we can decrement the count. | |
| 833 render_view_host_->GetProcess()->RemovePendingView(); | |
| 834 | |
| 835 // If the view is gone, then this RenderViewHost died while it was hidden. | |
| 836 // We ignored the RenderProcessGone call at the time, so we should send it now | |
| 837 // to make sure the sad tab shows up, etc. | |
| 838 if (!render_view_host_->GetView()) | |
| 839 delegate_->RenderProcessGoneFromRenderManager(render_view_host_); | |
| 840 else if (!delegate_->IsHidden()) | |
| 841 render_view_host_->GetView()->Show(); | |
| 842 | |
| 843 // Hide the old view now that the new one is visible. | |
| 844 if (old_render_view_host->GetView()) { | |
| 845 old_render_view_host->GetView()->Hide(); | |
| 846 old_render_view_host->WasSwappedOut(); | |
| 847 } | |
| 848 | |
| 849 // Make sure the size is up to date. (Fix for bug 1079768.) | |
| 850 delegate_->UpdateRenderViewSizeForRenderManager(); | |
| 851 | |
| 852 if (will_focus_location_bar) | |
| 853 delegate_->SetFocusToLocationBar(false); | |
| 854 else if (focus_render_view && render_view_host_->GetView()) | |
| 855 RenderWidgetHostViewPort::FromRWHV(render_view_host_->GetView())->Focus(); | |
| 856 | |
| 857 // Notify that we've swapped RenderViewHosts. We do this | |
| 858 // before shutting down the RVH so that we can clean up | |
| 859 // RendererResources related to the RVH first. | |
| 860 delegate_->NotifySwappedFromRenderManager(old_render_view_host, | |
| 861 render_view_host_); | |
| 862 | |
| 863 // If the pending view was on the swapped out list, we can remove it. | |
| 864 swapped_out_hosts_.erase(render_view_host_->GetSiteInstance()->GetId()); | |
| 865 | |
| 866 // If there are no active RVHs in this SiteInstance, it means that | |
| 867 // this RVH was the last active one in the SiteInstance. Now that we | |
| 868 // know that all RVHs are swapped out, we can delete all the RVHs in | |
| 869 // this SiteInstance. | |
| 870 if (!static_cast<SiteInstanceImpl*>(old_render_view_host->GetSiteInstance())-> | |
| 871 active_view_count()) { | |
| 872 ShutdownRenderViewHostsInSiteInstance( | |
| 873 old_render_view_host->GetSiteInstance()->GetId()); | |
| 874 // This is deleted while cleaning up the SitaInstance's views. | |
| 875 old_render_view_host = NULL; | |
| 876 } else if (old_render_view_host->IsRenderViewLive()) { | |
| 877 // If the old RVH is live, we are swapping it out and should keep track of | |
| 878 // it in case we navigate back to it. | |
| 879 DCHECK(old_render_view_host->is_swapped_out()); | |
| 880 // Temp fix for http://crbug.com/90867 until we do a better cleanup to make | |
| 881 // sure we don't get different rvh instances for the same site instance | |
| 882 // in the same rvhmgr. | |
| 883 // TODO(creis): Clean this up. | |
| 884 int32 old_site_instance_id = | |
| 885 old_render_view_host->GetSiteInstance()->GetId(); | |
| 886 RenderViewHostMap::iterator iter = | |
| 887 swapped_out_hosts_.find(old_site_instance_id); | |
| 888 if (iter != swapped_out_hosts_.end() && | |
| 889 iter->second != old_render_view_host) { | |
| 890 // Shutdown the RVH that will be replaced in the map to avoid a leak. | |
| 891 iter->second->Shutdown(); | |
| 892 } | |
| 893 swapped_out_hosts_[old_site_instance_id] = old_render_view_host; | |
| 894 } else { | |
| 895 old_render_view_host->Shutdown(); | |
| 896 old_render_view_host = NULL; // Shutdown() deletes it. | |
| 897 } | |
| 898 } | |
| 899 | |
| 900 void RenderViewHostManager::ShutdownRenderViewHostsInSiteInstance( | |
| 901 int32 site_instance_id) { | |
| 902 // First remove any swapped out RVH for this SiteInstance from our | |
| 903 // list. | |
| 904 swapped_out_hosts_.erase(site_instance_id); | |
| 905 | |
| 906 scoped_ptr<RenderWidgetHostIterator> widgets( | |
| 907 RenderWidgetHostImpl::GetAllRenderWidgetHosts()); | |
| 908 while (RenderWidgetHost* widget = widgets->GetNextHost()) { | |
| 909 if (!widget->IsRenderView()) | |
| 910 continue; | |
| 911 RenderViewHostImpl* rvh = | |
| 912 static_cast<RenderViewHostImpl*>(RenderViewHost::From(widget)); | |
| 913 if (site_instance_id == rvh->GetSiteInstance()->GetId()) | |
| 914 rvh->Shutdown(); | |
| 915 } | |
| 916 } | |
| 917 | |
| 918 RenderViewHostImpl* RenderViewHostManager::UpdateRendererStateForNavigate( | |
| 919 const NavigationEntryImpl& entry) { | |
| 920 // If we are currently navigating cross-process, we want to get back to normal | |
| 921 // and then navigate as usual. | |
| 922 if (cross_navigation_pending_) { | |
| 923 if (pending_render_view_host_) | |
| 924 CancelPending(); | |
| 925 cross_navigation_pending_ = false; | |
| 926 } | |
| 927 | |
| 928 // render_view_host_'s SiteInstance and new_instance will not be deleted | |
| 929 // before the end of this method, so we don't have to worry about their ref | |
| 930 // counts dropping to zero. | |
| 931 SiteInstance* current_instance = render_view_host_->GetSiteInstance(); | |
| 932 SiteInstance* new_instance = current_instance; | |
| 933 | |
| 934 // We do not currently swap processes for navigations in webview tag guests. | |
| 935 bool is_guest_scheme = current_instance->GetSiteURL().SchemeIs(kGuestScheme); | |
| 936 | |
| 937 // Determine if we need a new BrowsingInstance for this entry. If true, this | |
| 938 // implies that it will get a new SiteInstance (and likely process), and that | |
| 939 // other tabs in the current BrosingInstance will be unalbe to script it. | |
| 940 // This is used for cases that require a process swap even in the | |
| 941 // process-per-tab model, such as WebUI pages. | |
| 942 const NavigationEntry* current_entry = | |
| 943 delegate_->GetLastCommittedNavigationEntryForRenderManager(); | |
| 944 bool force_swap = !is_guest_scheme && | |
| 945 ShouldSwapBrowsingInstancesForNavigation(current_entry, &entry); | |
| 946 if (!is_guest_scheme && (ShouldTransitionCrossSite() || force_swap)) | |
| 947 new_instance = GetSiteInstanceForEntry(entry, current_instance, force_swap); | |
| 948 | |
| 949 // If force_swap is true, we must use a different SiteInstance. If we didn't, | |
| 950 // we would have two RenderViewHosts in the same SiteInstance and the same | |
| 951 // tab, resulting in page_id conflicts for their NavigationEntries. | |
| 952 if (force_swap) | |
| 953 CHECK_NE(new_instance, current_instance); | |
| 954 | |
| 955 if (new_instance != current_instance) { | |
| 956 // New SiteInstance: create a pending RVH to navigate. | |
| 957 DCHECK(!cross_navigation_pending_); | |
| 958 | |
| 959 // This will possibly create (set to NULL) a Web UI object for the pending | |
| 960 // page. We'll use this later to give the page special access. This must | |
| 961 // happen before the new renderer is created below so it will get bindings. | |
| 962 // It must also happen after the above conditional call to CancelPending(), | |
| 963 // otherwise CancelPending may clear the pending_web_ui_ and the page will | |
| 964 // not have its bindings set appropriately. | |
| 965 SetPendingWebUI(entry); | |
| 966 | |
| 967 // Ensure that we have created RVHs for the new RVH's opener chain if | |
| 968 // we are staying in the same BrowsingInstance. This allows the pending RVH | |
| 969 // to send cross-process script calls to its opener(s). | |
| 970 int opener_route_id = MSG_ROUTING_NONE; | |
| 971 if (new_instance->IsRelatedSiteInstance(current_instance)) { | |
| 972 opener_route_id = | |
| 973 delegate_->CreateOpenerRenderViewsForRenderManager(new_instance); | |
| 974 } | |
| 975 | |
| 976 // Create a non-swapped-out pending RVH with the given opener and navigate | |
| 977 // it. | |
| 978 int route_id = CreateRenderView(new_instance, opener_route_id, false, | |
| 979 delegate_->IsHidden()); | |
| 980 if (route_id == MSG_ROUTING_NONE) | |
| 981 return NULL; | |
| 982 | |
| 983 // Check if our current RVH is live before we set up a transition. | |
| 984 if (!render_view_host_->IsRenderViewLive()) { | |
| 985 if (!cross_navigation_pending_) { | |
| 986 // The current RVH is not live. There's no reason to sit around with a | |
| 987 // sad tab or a newly created RVH while we wait for the pending RVH to | |
| 988 // navigate. Just switch to the pending RVH now and go back to non | |
| 989 // cross-navigating (Note that we don't care about on{before}unload | |
| 990 // handlers if the current RVH isn't live.) | |
| 991 CommitPending(); | |
| 992 return render_view_host_; | |
| 993 } else { | |
| 994 NOTREACHED(); | |
| 995 return render_view_host_; | |
| 996 } | |
| 997 } | |
| 998 // Otherwise, it's safe to treat this as a pending cross-site transition. | |
| 999 | |
| 1000 // We need to wait until the beforeunload handler has run, unless we are | |
| 1001 // transferring an existing request (in which case it has already run). | |
| 1002 // Suspend the new render view (i.e., don't let it send the cross-site | |
| 1003 // Navigate message) until we hear back from the old renderer's | |
| 1004 // beforeunload handler. If the handler returns false, we'll have to | |
| 1005 // cancel the request. | |
| 1006 DCHECK(!pending_render_view_host_->are_navigations_suspended()); | |
| 1007 bool is_transfer = | |
| 1008 entry.transferred_global_request_id() != GlobalRequestID(); | |
| 1009 if (is_transfer) { | |
| 1010 // We don't need to stop the old renderer or run beforeunload/unload | |
| 1011 // handlers, because those have already been done. | |
| 1012 DCHECK(pending_nav_params_->global_request_id == | |
| 1013 entry.transferred_global_request_id()); | |
| 1014 } else { | |
| 1015 // Also make sure the old render view stops, in case a load is in | |
| 1016 // progress. (We don't want to do this for transfers, since it will | |
| 1017 // interrupt the transfer with an unexpected DidStopLoading.) | |
| 1018 render_view_host_->Send( | |
| 1019 new ViewMsg_Stop(render_view_host_->GetRoutingID())); | |
| 1020 | |
| 1021 pending_render_view_host_->SetNavigationsSuspended(true, | |
| 1022 base::TimeTicks()); | |
| 1023 | |
| 1024 // Tell the CrossSiteRequestManager that this RVH has a pending cross-site | |
| 1025 // request, so that ResourceDispatcherHost will know to tell us to run the | |
| 1026 // old page's unload handler before it sends the response. | |
| 1027 pending_render_view_host_->SetHasPendingCrossSiteRequest(true); | |
| 1028 } | |
| 1029 | |
| 1030 // We now have a pending RVH. | |
| 1031 DCHECK(!cross_navigation_pending_); | |
| 1032 cross_navigation_pending_ = true; | |
| 1033 | |
| 1034 // Unless we are transferring an existing request, we should now | |
| 1035 // tell the old render view to run its beforeunload handler, since it | |
| 1036 // doesn't otherwise know that the cross-site request is happening. This | |
| 1037 // will trigger a call to ShouldClosePage with the reply. | |
| 1038 if (!is_transfer) | |
| 1039 render_view_host_->FirePageBeforeUnload(true); | |
| 1040 | |
| 1041 return pending_render_view_host_; | |
| 1042 } | |
| 1043 | |
| 1044 // Otherwise the same SiteInstance can be used. Navigate render_view_host_. | |
| 1045 DCHECK(!cross_navigation_pending_); | |
| 1046 if (ShouldReuseWebUI(current_entry, &entry)) { | |
| 1047 pending_web_ui_.reset(); | |
| 1048 pending_and_current_web_ui_ = web_ui_->AsWeakPtr(); | |
| 1049 } else { | |
| 1050 SetPendingWebUI(entry); | |
| 1051 | |
| 1052 // Make sure the new RenderViewHost has the right bindings. | |
| 1053 if (pending_web_ui() && !render_view_host_->GetProcess()->IsGuest()) | |
| 1054 render_view_host_->AllowBindings(pending_web_ui()->GetBindings()); | |
| 1055 } | |
| 1056 | |
| 1057 if (pending_web_ui() && render_view_host_->IsRenderViewLive()) | |
| 1058 pending_web_ui()->GetController()->RenderViewReused(render_view_host_); | |
| 1059 | |
| 1060 // The renderer can exit view source mode when any error or cancellation | |
| 1061 // happen. We must overwrite to recover the mode. | |
| 1062 if (entry.IsViewSourceMode()) { | |
| 1063 render_view_host_->Send( | |
| 1064 new ViewMsg_EnableViewSourceMode(render_view_host_->GetRoutingID())); | |
| 1065 } | |
| 1066 | |
| 1067 return render_view_host_; | |
| 1068 } | |
| 1069 | |
| 1070 void RenderViewHostManager::CancelPending() { | |
| 1071 RenderViewHostImpl* pending_render_view_host = pending_render_view_host_; | |
| 1072 pending_render_view_host_ = NULL; | |
| 1073 | |
| 1074 RenderViewDevToolsAgentHost::OnCancelPendingNavigation( | |
| 1075 pending_render_view_host, | |
| 1076 render_view_host_); | |
| 1077 | |
| 1078 // We no longer need to prevent the process from exiting. | |
| 1079 pending_render_view_host->GetProcess()->RemovePendingView(); | |
| 1080 | |
| 1081 // The pending RVH may already be on the swapped out list if we started to | |
| 1082 // swap it back in and then canceled. If so, make sure it gets swapped out | |
| 1083 // again. If it's not on the swapped out list (e.g., aborting a pending | |
| 1084 // load), then it's safe to shut down. | |
| 1085 if (IsOnSwappedOutList(pending_render_view_host)) { | |
| 1086 // Any currently suspended navigations are no longer needed. | |
| 1087 pending_render_view_host->CancelSuspendedNavigations(); | |
| 1088 | |
| 1089 pending_render_view_host->SwapOut(); | |
| 1090 } else { | |
| 1091 // We won't be coming back, so shut this one down. | |
| 1092 pending_render_view_host->Shutdown(); | |
| 1093 } | |
| 1094 | |
| 1095 pending_web_ui_.reset(); | |
| 1096 pending_and_current_web_ui_.reset(); | |
| 1097 } | |
| 1098 | |
| 1099 void RenderViewHostManager::RenderViewDeleted(RenderViewHost* rvh) { | |
| 1100 // We are doing this in order to work around and to track a crasher | |
| 1101 // (http://crbug.com/23411) where it seems that pending_render_view_host_ is | |
| 1102 // deleted (not sure from where) but not NULLed. | |
| 1103 if (rvh == pending_render_view_host_) { | |
| 1104 // If you hit this NOTREACHED, please report it in the following bug | |
| 1105 // http://crbug.com/23411 Make sure to include what you were doing when it | |
| 1106 // happened (navigating to a new page, closing a tab...) and if you can | |
| 1107 // reproduce. | |
| 1108 NOTREACHED(); | |
| 1109 pending_render_view_host_ = NULL; | |
| 1110 } | |
| 1111 | |
| 1112 // Make sure deleted RVHs are not kept in the swapped out list while we are | |
| 1113 // still alive. (If render_view_host_ is null, we're already being deleted.) | |
| 1114 if (!render_view_host_) | |
| 1115 return; | |
| 1116 // We can't look it up by SiteInstance ID, which may no longer be valid. | |
| 1117 for (RenderViewHostMap::iterator iter = swapped_out_hosts_.begin(); | |
| 1118 iter != swapped_out_hosts_.end(); | |
| 1119 ++iter) { | |
| 1120 if (iter->second == rvh) { | |
| 1121 swapped_out_hosts_.erase(iter); | |
| 1122 break; | |
| 1123 } | |
| 1124 } | |
| 1125 } | |
| 1126 | |
| 1127 bool RenderViewHostManager::IsOnSwappedOutList(RenderViewHost* rvh) const { | |
| 1128 if (!rvh->GetSiteInstance()) | |
| 1129 return false; | |
| 1130 | |
| 1131 RenderViewHostMap::const_iterator iter = swapped_out_hosts_.find( | |
| 1132 rvh->GetSiteInstance()->GetId()); | |
| 1133 if (iter == swapped_out_hosts_.end()) | |
| 1134 return false; | |
| 1135 | |
| 1136 return iter->second == rvh; | |
| 1137 } | |
| 1138 | |
| 1139 RenderViewHostImpl* RenderViewHostManager::GetSwappedOutRenderViewHost( | |
| 1140 SiteInstance* instance) { | |
| 1141 RenderViewHostMap::iterator iter = swapped_out_hosts_.find(instance->GetId()); | |
| 1142 if (iter != swapped_out_hosts_.end()) | |
| 1143 return iter->second; | |
| 1144 | |
| 1145 return NULL; | |
| 1146 } | |
| 1147 | |
| 1148 } // namespace content | |
| OLD | NEW |