OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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 "chrome/browser/renderer_host/render_view_host_manager.h" | |
6 | |
7 #include "base/command_line.h" | |
8 #include "base/logging.h" | |
9 #include "chrome/browser/dom_ui/dom_ui.h" | |
10 #include "chrome/browser/dom_ui/dom_ui_factory.h" | |
11 #include "chrome/browser/renderer_host/render_view_host.h" | |
12 #include "chrome/browser/renderer_host/render_view_host_delegate.h" | |
13 #include "chrome/browser/renderer_host/render_view_host_factory.h" | |
14 #include "chrome/browser/renderer_host/render_widget_host_view.h" | |
15 #include "chrome/browser/renderer_host/site_instance.h" | |
16 #include "chrome/browser/tab_contents/navigation_controller.h" | |
17 #include "chrome/browser/tab_contents/navigation_entry.h" | |
18 #include "chrome/common/chrome_switches.h" | |
19 #include "chrome/common/notification_service.h" | |
20 #include "chrome/common/notification_type.h" | |
21 #include "chrome/common/render_messages.h" | |
22 #include "chrome/common/url_constants.h" | |
23 | |
24 namespace base { | |
25 class WaitableEvent; | |
26 } | |
27 | |
28 RenderViewHostManager::RenderViewHostManager( | |
29 RenderViewHostDelegate* render_view_delegate, | |
30 Delegate* delegate) | |
31 : delegate_(delegate), | |
32 cross_navigation_pending_(false), | |
33 render_view_delegate_(render_view_delegate), | |
34 render_view_host_(NULL), | |
35 pending_render_view_host_(NULL), | |
36 pending_renderer_aborted_(false), | |
37 interstitial_page_(NULL) { | |
38 registrar_.Add(this, NotificationType::RENDER_VIEW_HOST_DELETED, | |
39 NotificationService::AllSources()); | |
40 } | |
41 | |
42 RenderViewHostManager::~RenderViewHostManager() { | |
43 if (pending_render_view_host_) | |
44 CancelPending(); | |
45 | |
46 // We should always have a main RenderViewHost. | |
47 RenderViewHost* render_view_host = render_view_host_; | |
48 render_view_host_ = NULL; | |
49 render_view_host->Shutdown(); | |
50 } | |
51 | |
52 void RenderViewHostManager::Init(Profile* profile, | |
53 SiteInstance* site_instance, | |
54 int routing_id, | |
55 base::WaitableEvent* modal_dialog_event) { | |
56 // Create a RenderViewHost, once we have an instance. It is important to | |
57 // immediately give this SiteInstance to a RenderViewHost so that it is | |
58 // ref counted. | |
59 if (!site_instance) | |
60 site_instance = SiteInstance::CreateSiteInstance(profile); | |
61 render_view_host_ = RenderViewHostFactory::Create( | |
62 site_instance, render_view_delegate_, routing_id, modal_dialog_event); | |
63 NotificationService::current()->Notify( | |
64 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB, | |
65 Source<RenderViewHostManager>(this), | |
66 Details<RenderViewHost>(render_view_host_)); | |
67 } | |
68 | |
69 RenderViewHost* RenderViewHostManager::Navigate(const NavigationEntry& entry) { | |
70 // Create a pending RenderViewHost. It will give us the one we should use | |
71 RenderViewHost* dest_render_view_host = UpdateRendererStateForNavigate(entry); | |
72 if (!dest_render_view_host) | |
73 return NULL; // We weren't able to create a pending render view host. | |
74 | |
75 // If the current render_view_host_ isn't live, we should create it so | |
76 // that we don't show a sad tab while the dest_render_view_host fetches | |
77 // its first page. (Bug 1145340) | |
78 if (dest_render_view_host != render_view_host_ && | |
79 !render_view_host_->IsRenderViewLive()) { | |
80 delegate_->CreateRenderViewForRenderManager(render_view_host_); | |
81 } | |
82 | |
83 // If the renderer crashed, then try to create a new one to satisfy this | |
84 // navigation request. | |
85 if (!dest_render_view_host->IsRenderViewLive()) { | |
86 if (!delegate_->CreateRenderViewForRenderManager(dest_render_view_host)) | |
87 return NULL; | |
88 | |
89 // Now that we've created a new renderer, be sure to hide it if it isn't | |
90 // our primary one. Otherwise, we might crash if we try to call Show() | |
91 // on it later. | |
92 if (dest_render_view_host != render_view_host_ && | |
93 dest_render_view_host->view()) { | |
94 dest_render_view_host->view()->Hide(); | |
95 } else { | |
96 // This is our primary renderer, notify here as we won't be calling | |
97 // CommitPending (which does the notify). | |
98 RenderViewHostSwitchedDetails details; | |
99 details.new_host = render_view_host_; | |
100 details.old_host = NULL; | |
101 NotificationService::current()->Notify( | |
102 NotificationType::RENDER_VIEW_HOST_CHANGED, | |
103 Source<NavigationController>( | |
104 &delegate_->GetControllerForRenderManager()), | |
105 Details<RenderViewHostSwitchedDetails>(&details)); | |
106 } | |
107 } | |
108 | |
109 return dest_render_view_host; | |
110 } | |
111 | |
112 void RenderViewHostManager::Stop() { | |
113 render_view_host_->Stop(); | |
114 | |
115 // If we are cross-navigating, we should stop the pending renderers. This | |
116 // will lead to a DidFailProvisionalLoad, which will properly destroy them. | |
117 if (cross_navigation_pending_) | |
118 pending_render_view_host_->Stop(); | |
119 } | |
120 | |
121 void RenderViewHostManager::SetIsLoading(bool is_loading) { | |
122 render_view_host_->SetIsLoading(is_loading); | |
123 if (pending_render_view_host_) | |
124 pending_render_view_host_->SetIsLoading(is_loading); | |
125 } | |
126 | |
127 bool RenderViewHostManager::ShouldCloseTabOnUnresponsiveRenderer() { | |
128 if (!cross_navigation_pending_) | |
129 return true; | |
130 | |
131 // If the tab becomes unresponsive during unload while doing a | |
132 // cross-site navigation, proceed with the navigation. (This assumes that | |
133 // the pending RenderViewHost is still responsive.) | |
134 int pending_request_id = pending_render_view_host_->GetPendingRequestId(); | |
135 if (pending_request_id == -1) { | |
136 // Haven't gotten around to starting the request, because we're still | |
137 // waiting for the beforeunload handler to finish. We'll pretend that it | |
138 // did finish, to let the navigation proceed. Note that there's a danger | |
139 // that the beforeunload handler will later finish and possibly return | |
140 // false (meaning the navigation should not proceed), but we'll ignore it | |
141 // in this case because it took too long. | |
142 if (pending_render_view_host_->are_navigations_suspended()) | |
143 pending_render_view_host_->SetNavigationsSuspended(false); | |
144 } else { | |
145 // The request has been started and paused, while we're waiting for the | |
146 // unload handler to finish. We'll pretend that it did, by notifying the | |
147 // IO thread to let the response continue. The pending renderer will then | |
148 // be swapped in as part of the usual DidNavigate logic. (If the unload | |
149 // handler later finishes, this call will be ignored because the state in | |
150 // CrossSiteResourceHandler will already be cleaned up.) | |
151 current_host()->process()->CrossSiteClosePageACK( | |
152 pending_render_view_host_->process()->pid(), pending_request_id); | |
153 } | |
154 return false; | |
155 } | |
156 | |
157 void RenderViewHostManager::DidNavigateMainFrame( | |
158 RenderViewHost* render_view_host) { | |
159 if (!cross_navigation_pending_) { | |
160 DCHECK(!pending_render_view_host_); | |
161 | |
162 // We should only hear this from our current renderer. | |
163 DCHECK(render_view_host == render_view_host_); | |
164 | |
165 // Even when there is no pending RVH, there may be a pending DOM UI. | |
166 if (pending_dom_ui_.get()) | |
167 CommitPending(); | |
168 return; | |
169 } | |
170 | |
171 if (render_view_host == pending_render_view_host_) { | |
172 // The pending cross-site navigation completed, so show the renderer. | |
173 CommitPending(); | |
174 cross_navigation_pending_ = false; | |
175 } else if (render_view_host == render_view_host_) { | |
176 // A navigation in the original page has taken place. Cancel the pending | |
177 // one. | |
178 CancelPending(); | |
179 cross_navigation_pending_ = false; | |
180 } else { | |
181 // No one else should be sending us DidNavigate in this state. | |
182 DCHECK(false); | |
183 } | |
184 } | |
185 | |
186 void RenderViewHostManager::OnCrossSiteResponse(int new_render_process_host_id, | |
187 int new_request_id) { | |
188 // Should only see this while we have a pending renderer. | |
189 if (!cross_navigation_pending_) | |
190 return; | |
191 DCHECK(pending_render_view_host_); | |
192 | |
193 // Tell the old renderer to run its onunload handler. When it finishes, it | |
194 // will send a ClosePage_ACK to the ResourceDispatcherHost with the given | |
195 // IDs (of the pending RVH's request), allowing the pending RVH's response to | |
196 // resume. | |
197 render_view_host_->ClosePage(new_render_process_host_id, new_request_id); | |
198 | |
199 // ResourceDispatcherHost has told us to run the onunload handler, which | |
200 // means it is not a download or unsafe page, and we are going to perform the | |
201 // navigation. Thus, we no longer need to remember that the RenderViewHost | |
202 // is part of a pending cross-site request. | |
203 pending_render_view_host_->SetHasPendingCrossSiteRequest(false, | |
204 new_request_id); | |
205 } | |
206 | |
207 void RenderViewHostManager::RendererAbortedProvisionalLoad( | |
208 RenderViewHost* render_view_host) { | |
209 // We used to cancel the pending renderer here for cross-site downloads. | |
210 // However, it's not safe to do that because the download logic repeatedly | |
211 // looks for this TabContents based on a render view ID. Instead, we just | |
212 // leave the pending renderer around until the next navigation event | |
213 // (Navigate, DidNavigate, etc), which will clean it up properly. | |
214 // TODO(creis): All of this will go away when we move the cross-site logic | |
215 // to ResourceDispatcherHost, so that we intercept responses rather than | |
216 // navigation events. (That's necessary to support onunload anyway.) Once | |
217 // we've made that change, we won't create a pending renderer until we know | |
218 // the response is not a download. | |
219 | |
220 // There is one instance where we must be able to pre-emptively clean up a | |
221 // pending renderer: If a cross-site download is initiated from a chrome:// | |
222 // url, and the browser then wants to close. | |
223 if (pending_render_view_host_) { | |
224 pending_renderer_aborted_ = true; | |
225 } | |
226 } | |
227 | |
228 void RenderViewHostManager::ShouldClosePage(bool proceed) { | |
229 // Should only see this while we have a pending renderer. Otherwise, we | |
230 // should ignore. | |
231 if (pending_render_view_host_ && pending_renderer_aborted_) | |
232 CancelPending(); | |
233 | |
234 if (!pending_render_view_host_) { | |
235 bool proceed_to_fire_unload; | |
236 delegate_->BeforeUnloadFiredFromRenderManager(proceed, | |
237 &proceed_to_fire_unload); | |
238 | |
239 if (proceed_to_fire_unload) { | |
240 // This is not a cross-site navigation, the tab is being closed. | |
241 render_view_host_->FirePageUnload(); | |
242 } | |
243 return; | |
244 } | |
245 | |
246 if (proceed) { | |
247 // Ok to unload the current page, so proceed with the cross-site | |
248 // navigation. Note that if navigations are not currently suspended, it | |
249 // might be because the renderer was deemed unresponsive and this call was | |
250 // already made by ShouldCloseTabOnUnresponsiveRenderer. In that case, it | |
251 // is ok to do nothing here. | |
252 if (pending_render_view_host_->are_navigations_suspended()) | |
253 pending_render_view_host_->SetNavigationsSuspended(false); | |
254 } else { | |
255 // Current page says to cancel. | |
256 CancelPending(); | |
257 cross_navigation_pending_ = false; | |
258 } | |
259 } | |
260 | |
261 void RenderViewHostManager::OnJavaScriptMessageBoxClosed( | |
262 IPC::Message* reply_msg, | |
263 bool success, | |
264 const std::wstring& prompt) { | |
265 render_view_host_->JavaScriptMessageBoxClosed(reply_msg, success, prompt); | |
266 } | |
267 | |
268 void RenderViewHostManager::OnJavaScriptMessageBoxWindowDestroyed() { | |
269 render_view_host_->JavaScriptMessageBoxWindowDestroyed(); | |
270 } | |
271 | |
272 void RenderViewHostManager::Observe(NotificationType type, | |
273 const NotificationSource& source, | |
274 const NotificationDetails& details) { | |
275 // Debugging code to help isolate | |
276 // http://code.google.com/p/chromium/issues/detail?id=6316 . We should never | |
277 // reference a RVH that is about to be deleted. | |
278 RenderViewHost* deleted_rvh = Source<RenderViewHost>(source).ptr(); | |
279 CHECK(deleted_rvh); | |
280 CHECK(render_view_host_ != deleted_rvh); | |
281 CHECK(pending_render_view_host_ != deleted_rvh); | |
282 } | |
283 | |
284 bool RenderViewHostManager::ShouldTransitionCrossSite() { | |
285 // True if we are using process-per-site-instance (default) or | |
286 // process-per-site (kProcessPerSite). | |
287 return !CommandLine::ForCurrentProcess()->HasSwitch(switches::kProcessPerTab); | |
288 } | |
289 | |
290 bool RenderViewHostManager::ShouldSwapProcessesForNavigation( | |
291 const NavigationEntry* cur_entry, | |
292 const NavigationEntry* new_entry) const { | |
293 if (!cur_entry || !new_entry) | |
294 return false; | |
295 | |
296 // We can't switch a RenderView between view source and non-view source mode | |
297 // without screwing up the session history sometimes (when navigating between | |
298 // "view-source:http://foo.com/" and "http://foo.com/", WebKit doesn't treat | |
299 // it as a new navigation). So require a view switch. | |
300 if (cur_entry->IsViewSourceMode() != new_entry->IsViewSourceMode()) | |
301 return true; | |
302 | |
303 // For security, we should transition between processes when one is a DOM UI | |
304 // page and one isn't. | |
305 if (DOMUIFactory::HasDOMUIScheme(cur_entry->url()) != | |
306 DOMUIFactory::HasDOMUIScheme(new_entry->url())) | |
307 return true; | |
308 | |
309 // Also, we must switch if one is an extension and the other is not the exact | |
310 // same extension. | |
311 if (cur_entry->url().SchemeIs(chrome::kExtensionScheme) || | |
312 new_entry->url().SchemeIs(chrome::kExtensionScheme)) | |
313 if (cur_entry->url().GetOrigin() != new_entry->url().GetOrigin()) | |
314 return true; | |
315 | |
316 | |
317 return false; | |
318 } | |
319 | |
320 SiteInstance* RenderViewHostManager::GetSiteInstanceForEntry( | |
321 const NavigationEntry& entry, | |
322 SiteInstance* curr_instance) { | |
323 // NOTE: This is only called when ShouldTransitionCrossSite is true. | |
324 | |
325 // If the entry has an instance already, we should use it. | |
326 if (entry.site_instance()) | |
327 return entry.site_instance(); | |
328 | |
329 // (UGLY) HEURISTIC, process-per-site only: | |
330 // | |
331 // If this navigation is generated, then it probably corresponds to a search | |
332 // query. Given that search results typically lead to users navigating to | |
333 // other sites, we don't really want to use the search engine hostname to | |
334 // determine the site instance for this navigation. | |
335 // | |
336 // NOTE: This can be removed once we have a way to transition between | |
337 // RenderViews in response to a link click. | |
338 // | |
339 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kProcessPerSite) && | |
340 entry.transition_type() == PageTransition::GENERATED) | |
341 return curr_instance; | |
342 | |
343 const GURL& dest_url = entry.url(); | |
344 | |
345 // If we haven't used our SiteInstance (and thus RVH) yet, then we can use it | |
346 // for this entry. We won't commit the SiteInstance to this site until the | |
347 // navigation commits (in DidNavigate), unless the navigation entry was | |
348 // restored. As session restore loads all the pages immediately we need to set | |
349 // the site first, otherwise after a restore none of the pages would share | |
350 // renderers. | |
351 if (!curr_instance->has_site()) { | |
352 // If we've already created a SiteInstance for our destination, we don't | |
353 // want to use this unused SiteInstance; use the existing one. (We don't | |
354 // do this check if the curr_instance has a site, because for now, we want | |
355 // to compare against the current URL and not the SiteInstance's site. In | |
356 // this case, there is no current URL, so comparing against the site is ok. | |
357 // See additional comments below.) | |
358 if (curr_instance->HasRelatedSiteInstance(dest_url)) { | |
359 return curr_instance->GetRelatedSiteInstance(dest_url); | |
360 } else { | |
361 if (entry.restored()) | |
362 curr_instance->SetSite(dest_url); | |
363 return curr_instance; | |
364 } | |
365 } | |
366 | |
367 // Otherwise, only create a new SiteInstance for cross-site navigation. | |
368 | |
369 // TODO(creis): Once we intercept links and script-based navigations, we | |
370 // will be able to enforce that all entries in a SiteInstance actually have | |
371 // the same site, and it will be safe to compare the URL against the | |
372 // SiteInstance's site, as follows: | |
373 // const GURL& current_url = curr_instance->site(); | |
374 // For now, though, we're in a hybrid model where you only switch | |
375 // SiteInstances if you type in a cross-site URL. This means we have to | |
376 // compare the entry's URL to the last committed entry's URL. | |
377 NavigationController& controller = delegate_->GetControllerForRenderManager(); | |
378 NavigationEntry* curr_entry = controller.GetLastCommittedEntry(); | |
379 if (interstitial_page_) { | |
380 // The interstitial is currently the last committed entry, but we want to | |
381 // compare against the last non-interstitial entry. | |
382 curr_entry = controller.GetEntryAtOffset(-1); | |
383 } | |
384 // If there is no last non-interstitial entry (and curr_instance already | |
385 // has a site), then we must have been opened from another tab. We want | |
386 // to compare against the URL of the page that opened us, but we can't | |
387 // get to it directly. The best we can do is check against the site of | |
388 // the SiteInstance. This will be correct when we intercept links and | |
389 // script-based navigations, but for now, it could place some pages in a | |
390 // new process unnecessarily. We should only hit this case if a page tries | |
391 // to open a new tab to an interstitial-inducing URL, and then navigates | |
392 // the page to a different same-site URL. (This seems very unlikely in | |
393 // practice.) | |
394 const GURL& current_url = (curr_entry) ? curr_entry->url() : | |
395 curr_instance->site(); | |
396 | |
397 if (SiteInstance::IsSameWebSite(current_url, dest_url)) { | |
398 return curr_instance; | |
399 } else if (ShouldSwapProcessesForNavigation(curr_entry, &entry)) { | |
400 // When we're swapping, we need to force the site instance AND browsing | |
401 // instance to be different ones. This addresses special cases where we use | |
402 // a single BrowsingInstance for all pages of a certain type (e.g., New Tab | |
403 // Pages), keeping them in the same process. When you navigate away from | |
404 // that page, we want to explicity ignore that BrowsingInstance and group | |
405 // this page into the appropriate SiteInstance for its URL. | |
406 return SiteInstance::CreateSiteInstanceForURL( | |
407 delegate_->GetControllerForRenderManager().profile(), dest_url); | |
408 } else { | |
409 // Start the new renderer in a new SiteInstance, but in the current | |
410 // BrowsingInstance. It is important to immediately give this new | |
411 // SiteInstance to a RenderViewHost (if it is different than our current | |
412 // SiteInstance), so that it is ref counted. This will happen in | |
413 // CreatePendingRenderView. | |
414 return curr_instance->GetRelatedSiteInstance(dest_url); | |
415 } | |
416 } | |
417 | |
418 bool RenderViewHostManager::CreatePendingRenderView(SiteInstance* instance) { | |
419 NavigationEntry* curr_entry = | |
420 delegate_->GetControllerForRenderManager().GetLastCommittedEntry(); | |
421 if (curr_entry) { | |
422 DCHECK(!curr_entry->content_state().empty()); | |
423 // TODO(creis): Should send a message to the RenderView to let it know | |
424 // we're about to switch away, so that it sends an UpdateState message. | |
425 } | |
426 | |
427 pending_render_view_host_ = RenderViewHostFactory::Create( | |
428 instance, render_view_delegate_, MSG_ROUTING_NONE, NULL); | |
429 NotificationService::current()->Notify( | |
430 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB, | |
431 Source<RenderViewHostManager>(this), | |
432 Details<RenderViewHost>(pending_render_view_host_)); | |
433 | |
434 bool success = delegate_->CreateRenderViewForRenderManager( | |
435 pending_render_view_host_); | |
436 if (success) { | |
437 // Don't show the view until we get a DidNavigate from it. | |
438 pending_render_view_host_->view()->Hide(); | |
439 } else { | |
440 CancelPending(); | |
441 } | |
442 return success; | |
443 } | |
444 | |
445 void RenderViewHostManager::CommitPending() { | |
446 // First commit the DOM UI, if any. | |
447 dom_ui_.swap(pending_dom_ui_); | |
448 pending_dom_ui_.reset(); | |
449 | |
450 // It's possible for the pending_render_view_host_ to be NULL when we aren't | |
451 // crossing process boundaries. If so, we just needed to handle the DOM UI | |
452 // committing above and we're done. | |
453 if (!pending_render_view_host_) | |
454 return; | |
455 | |
456 // Remember if the page was focused so we can focus the new renderer in | |
457 // that case. | |
458 bool focus_render_view = render_view_host_->view() && | |
459 render_view_host_->view()->HasFocus(); | |
460 | |
461 // Hide the current view and prepare to destroy it. | |
462 // TODO(creis): Get the old RenderViewHost to send us an UpdateState message | |
463 // before we destroy it. | |
464 if (render_view_host_->view()) | |
465 render_view_host_->view()->Hide(); | |
466 RenderViewHost* old_render_view_host = render_view_host_; | |
467 | |
468 // Swap in the pending view and make it active. | |
469 render_view_host_ = pending_render_view_host_; | |
470 pending_render_view_host_ = NULL; | |
471 | |
472 // If the view is gone, then this RenderViewHost died while it was hidden. | |
473 // We ignored the RenderViewGone call at the time, so we should send it now | |
474 // to make sure the sad tab shows up, etc. | |
475 if (render_view_host_->view()) | |
476 render_view_host_->view()->Show(); | |
477 else | |
478 delegate_->RenderViewGoneFromRenderManager(render_view_host_); | |
479 | |
480 // Make sure the size is up to date. (Fix for bug 1079768.) | |
481 delegate_->UpdateRenderViewSizeForRenderManager(); | |
482 | |
483 if (focus_render_view && render_view_host_->view()) | |
484 render_view_host_->view()->Focus(); | |
485 | |
486 RenderViewHostSwitchedDetails details; | |
487 details.new_host = render_view_host_; | |
488 details.old_host = old_render_view_host; | |
489 NotificationService::current()->Notify( | |
490 NotificationType::RENDER_VIEW_HOST_CHANGED, | |
491 Source<NavigationController>(&delegate_->GetControllerForRenderManager()), | |
492 Details<RenderViewHostSwitchedDetails>(&details)); | |
493 | |
494 old_render_view_host->Shutdown(); | |
495 | |
496 // Let the task manager know that we've swapped RenderViewHosts, since it | |
497 // might need to update its process groupings. | |
498 delegate_->NotifySwappedFromRenderManager(); | |
499 } | |
500 | |
501 RenderViewHost* RenderViewHostManager::UpdateRendererStateForNavigate( | |
502 const NavigationEntry& entry) { | |
503 // If we are cross-navigating, then we want to get back to normal and navigate | |
504 // as usual. | |
505 if (cross_navigation_pending_) { | |
506 if (pending_render_view_host_) | |
507 CancelPending(); | |
508 cross_navigation_pending_ = false; | |
509 } | |
510 | |
511 // This will possibly create (set to NULL) a DOM UI object for the pending | |
512 // page. We'll use this later to give the page special access. This must | |
513 // happen before the new renderer is created below so it will get bindings. | |
514 // It must also happen after the above conditional call to CancelPending(), | |
515 // otherwise CancelPending may clear the pending_dom_ui_ and the page will | |
516 // not have it's bindings set appropriately. | |
517 pending_dom_ui_.reset(delegate_->CreateDOMUIForRenderManager(entry.url())); | |
518 | |
519 // render_view_host_ will not be deleted before the end of this method, so we | |
520 // don't have to worry about this SiteInstance's ref count dropping to zero. | |
521 SiteInstance* curr_instance = render_view_host_->site_instance(); | |
522 | |
523 // Determine if we need a new SiteInstance for this entry. | |
524 // Again, new_instance won't be deleted before the end of this method, so it | |
525 // is safe to use a normal pointer here. | |
526 SiteInstance* new_instance = curr_instance; | |
527 if (ShouldTransitionCrossSite()) | |
528 new_instance = GetSiteInstanceForEntry(entry, curr_instance); | |
529 | |
530 if (new_instance != curr_instance || | |
531 ShouldSwapProcessesForNavigation( | |
532 delegate_->GetLastCommittedNavigationEntryForRenderManager(), | |
533 &entry)) { | |
534 // New SiteInstance. | |
535 DCHECK(!cross_navigation_pending_); | |
536 | |
537 // Create a pending RVH and navigate it. | |
538 bool success = CreatePendingRenderView(new_instance); | |
539 if (!success) | |
540 return NULL; | |
541 | |
542 // Check if our current RVH is live before we set up a transition. | |
543 if (!render_view_host_->IsRenderViewLive()) { | |
544 if (!cross_navigation_pending_) { | |
545 // The current RVH is not live. There's no reason to sit around with a | |
546 // sad tab or a newly created RVH while we wait for the pending RVH to | |
547 // navigate. Just switch to the pending RVH now and go back to non | |
548 // cross-navigating (Note that we don't care about on{before}unload | |
549 // handlers if the current RVH isn't live.) | |
550 CommitPending(); | |
551 return render_view_host_; | |
552 } else { | |
553 NOTREACHED(); | |
554 return render_view_host_; | |
555 } | |
556 } | |
557 // Otherwise, it's safe to treat this as a pending cross-site transition. | |
558 | |
559 // Make sure the old render view stops, in case a load is in progress. | |
560 render_view_host_->Stop(); | |
561 | |
562 // Suspend the new render view (i.e., don't let it send the cross-site | |
563 // Navigate message) until we hear back from the old renderer's | |
564 // onbeforeunload handler. If the handler returns false, we'll have to | |
565 // cancel the request. | |
566 DCHECK(!pending_render_view_host_->are_navigations_suspended()); | |
567 pending_render_view_host_->SetNavigationsSuspended(true); | |
568 | |
569 // Tell the CrossSiteRequestManager that this RVH has a pending cross-site | |
570 // request, so that ResourceDispatcherHost will know to tell us to run the | |
571 // old page's onunload handler before it sends the response. | |
572 pending_render_view_host_->SetHasPendingCrossSiteRequest(true, -1); | |
573 | |
574 // We now have a pending RVH. | |
575 DCHECK(!cross_navigation_pending_); | |
576 cross_navigation_pending_ = true; | |
577 | |
578 // Tell the old render view to run its onbeforeunload handler, since it | |
579 // doesn't otherwise know that the cross-site request is happening. This | |
580 // will trigger a call to ShouldClosePage with the reply. | |
581 render_view_host_->FirePageBeforeUnload(); | |
582 | |
583 return pending_render_view_host_; | |
584 } else { | |
585 // The renderer can exit view source mode when any error or cancellation | |
586 // happen. We must overwrite to recover the mode. | |
587 if (entry.IsViewSourceMode()) { | |
588 render_view_host_->Send( | |
589 new ViewMsg_EnableViewSourceMode(render_view_host_->routing_id())); | |
590 } | |
591 } | |
592 | |
593 // Same SiteInstance can be used. Navigate render_view_host_ if we are not | |
594 // cross navigating. | |
595 DCHECK(!cross_navigation_pending_); | |
596 return render_view_host_; | |
597 } | |
598 | |
599 void RenderViewHostManager::CancelPending() { | |
600 RenderViewHost* pending_render_view_host = pending_render_view_host_; | |
601 pending_render_view_host_ = NULL; | |
602 pending_render_view_host->Shutdown(); | |
603 pending_renderer_aborted_ = false; | |
604 pending_dom_ui_.reset(); | |
605 } | |
606 | |
607 void RenderViewHostManager::CrossSiteNavigationCanceled() { | |
608 DCHECK(cross_navigation_pending_); | |
609 cross_navigation_pending_ = false; | |
610 if (pending_render_view_host_) | |
611 CancelPending(); | |
612 } | |
OLD | NEW |