OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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/tab_contents/render_view_host_manager.h" | |
6 | |
7 #include "base/command_line.h" | |
8 #include "base/logging.h" | |
9 #include "chrome/browser/extensions/extension_service.h" | |
10 #include "chrome/browser/profiles/profile.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/browser/tab_contents/tab_contents_view.h" | |
19 #include "chrome/browser/webui/web_ui.h" | |
20 #include "chrome/browser/webui/web_ui_factory.h" | |
21 #include "chrome/common/chrome_switches.h" | |
22 #include "chrome/common/notification_service.h" | |
23 #include "chrome/common/notification_type.h" | |
24 #include "chrome/common/render_messages.h" | |
25 #include "chrome/common/render_messages_params.h" | |
26 #include "chrome/common/url_constants.h" | |
27 | |
28 namespace base { | |
29 class WaitableEvent; | |
30 } | |
31 | |
32 RenderViewHostManager::RenderViewHostManager( | |
33 RenderViewHostDelegate* render_view_delegate, | |
34 Delegate* delegate) | |
35 : delegate_(delegate), | |
36 cross_navigation_pending_(false), | |
37 render_view_delegate_(render_view_delegate), | |
38 render_view_host_(NULL), | |
39 pending_render_view_host_(NULL), | |
40 interstitial_page_(NULL) { | |
41 } | |
42 | |
43 RenderViewHostManager::~RenderViewHostManager() { | |
44 if (pending_render_view_host_) | |
45 CancelPending(); | |
46 | |
47 // We should always have a main RenderViewHost. | |
48 RenderViewHost* render_view_host = render_view_host_; | |
49 render_view_host_ = NULL; | |
50 render_view_host->Shutdown(); | |
51 } | |
52 | |
53 void RenderViewHostManager::Init(Profile* profile, | |
54 SiteInstance* site_instance, | |
55 int routing_id) { | |
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, delegate_-> | |
63 GetControllerForRenderManager().session_storage_namespace()); | |
64 NotificationService::current()->Notify( | |
65 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB, | |
66 Source<RenderViewHostManager>(this), | |
67 Details<RenderViewHost>(render_view_host_)); | |
68 } | |
69 | |
70 RenderWidgetHostView* RenderViewHostManager::GetRenderWidgetHostView() const { | |
71 if (!render_view_host_) | |
72 return NULL; | |
73 return render_view_host_->view(); | |
74 } | |
75 | |
76 RenderViewHost* RenderViewHostManager::Navigate(const NavigationEntry& entry) { | |
77 // Create a pending RenderViewHost. It will give us the one we should use | |
78 RenderViewHost* dest_render_view_host = UpdateRendererStateForNavigate(entry); | |
79 if (!dest_render_view_host) | |
80 return NULL; // We weren't able to create a pending render view host. | |
81 | |
82 // If the current render_view_host_ isn't live, we should create it so | |
83 // that we don't show a sad tab while the dest_render_view_host fetches | |
84 // its first page. (Bug 1145340) | |
85 if (dest_render_view_host != render_view_host_ && | |
86 !render_view_host_->IsRenderViewLive()) { | |
87 // Note: we don't call InitRenderView here because we are navigating away | |
88 // soon anyway, and we don't have the NavigationEntry for this host. | |
89 delegate_->CreateRenderViewForRenderManager(render_view_host_); | |
90 } | |
91 | |
92 // If the renderer crashed, then try to create a new one to satisfy this | |
93 // navigation request. | |
94 if (!dest_render_view_host->IsRenderViewLive()) { | |
95 if (!InitRenderView(dest_render_view_host, entry)) | |
96 return NULL; | |
97 | |
98 // Now that we've created a new renderer, be sure to hide it if it isn't | |
99 // our primary one. Otherwise, we might crash if we try to call Show() | |
100 // on it later. | |
101 if (dest_render_view_host != render_view_host_ && | |
102 dest_render_view_host->view()) { | |
103 dest_render_view_host->view()->Hide(); | |
104 } else { | |
105 // This is our primary renderer, notify here as we won't be calling | |
106 // CommitPending (which does the notify). | |
107 RenderViewHostSwitchedDetails details; | |
108 details.new_host = render_view_host_; | |
109 details.old_host = NULL; | |
110 NotificationService::current()->Notify( | |
111 NotificationType::RENDER_VIEW_HOST_CHANGED, | |
112 Source<NavigationController>( | |
113 &delegate_->GetControllerForRenderManager()), | |
114 Details<RenderViewHostSwitchedDetails>(&details)); | |
115 } | |
116 } | |
117 | |
118 return dest_render_view_host; | |
119 } | |
120 | |
121 void RenderViewHostManager::Stop() { | |
122 render_view_host_->Stop(); | |
123 | |
124 // If we are cross-navigating, we should stop the pending renderers. This | |
125 // will lead to a DidFailProvisionalLoad, which will properly destroy them. | |
126 if (cross_navigation_pending_) | |
127 pending_render_view_host_->Stop(); | |
128 } | |
129 | |
130 void RenderViewHostManager::SetIsLoading(bool is_loading) { | |
131 render_view_host_->SetIsLoading(is_loading); | |
132 if (pending_render_view_host_) | |
133 pending_render_view_host_->SetIsLoading(is_loading); | |
134 } | |
135 | |
136 bool RenderViewHostManager::ShouldCloseTabOnUnresponsiveRenderer() { | |
137 if (!cross_navigation_pending_) | |
138 return true; | |
139 | |
140 // If the tab becomes unresponsive during unload while doing a | |
141 // cross-site navigation, proceed with the navigation. (This assumes that | |
142 // the pending RenderViewHost is still responsive.) | |
143 int pending_request_id = pending_render_view_host_->GetPendingRequestId(); | |
144 if (pending_request_id == -1) { | |
145 // Haven't gotten around to starting the request, because we're still | |
146 // waiting for the beforeunload handler to finish. We'll pretend that it | |
147 // did finish, to let the navigation proceed. Note that there's a danger | |
148 // that the beforeunload handler will later finish and possibly return | |
149 // false (meaning the navigation should not proceed), but we'll ignore it | |
150 // in this case because it took too long. | |
151 if (pending_render_view_host_->are_navigations_suspended()) | |
152 pending_render_view_host_->SetNavigationsSuspended(false); | |
153 } else { | |
154 // The request has been started and paused, while we're waiting for the | |
155 // unload handler to finish. We'll pretend that it did, by notifying the | |
156 // IO thread to let the response continue. The pending renderer will then | |
157 // be swapped in as part of the usual DidNavigate logic. (If the unload | |
158 // handler later finishes, this call will be ignored because the state in | |
159 // CrossSiteResourceHandler will already be cleaned up.) | |
160 ViewMsg_ClosePage_Params params; | |
161 params.closing_process_id = | |
162 render_view_host_->process()->id(); | |
163 params.closing_route_id = render_view_host_->routing_id(); | |
164 params.for_cross_site_transition = true; | |
165 params.new_render_process_host_id = | |
166 pending_render_view_host_->process()->id(); | |
167 params.new_request_id = pending_request_id; | |
168 current_host()->process()->CrossSiteClosePageACK(params); | |
169 } | |
170 return false; | |
171 } | |
172 | |
173 void RenderViewHostManager::DidNavigateMainFrame( | |
174 RenderViewHost* render_view_host) { | |
175 if (!cross_navigation_pending_) { | |
176 DCHECK(!pending_render_view_host_); | |
177 | |
178 // We should only hear this from our current renderer. | |
179 DCHECK(render_view_host == render_view_host_); | |
180 | |
181 // Even when there is no pending RVH, there may be a pending Web UI. | |
182 if (pending_web_ui_.get()) | |
183 CommitPending(); | |
184 return; | |
185 } | |
186 | |
187 if (render_view_host == pending_render_view_host_) { | |
188 // The pending cross-site navigation completed, so show the renderer. | |
189 CommitPending(); | |
190 cross_navigation_pending_ = false; | |
191 } else if (render_view_host == render_view_host_) { | |
192 // A navigation in the original page has taken place. Cancel the pending | |
193 // one. | |
194 CancelPending(); | |
195 cross_navigation_pending_ = false; | |
196 } else { | |
197 // No one else should be sending us DidNavigate in this state. | |
198 DCHECK(false); | |
199 } | |
200 } | |
201 | |
202 void RenderViewHostManager::SetWebUIPostCommit(WebUI* web_ui) { | |
203 DCHECK(!web_ui_.get()); | |
204 web_ui_.reset(web_ui); | |
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 | |
221 void RenderViewHostManager::ShouldClosePage(bool for_cross_site_transition, | |
222 bool proceed) { | |
223 if (for_cross_site_transition) { | |
224 // Ignore if we're not in a cross-site navigation. | |
225 if (!cross_navigation_pending_) | |
226 return; | |
227 | |
228 if (proceed) { | |
229 // Ok to unload the current page, so proceed with the cross-site | |
230 // navigation. Note that if navigations are not currently suspended, it | |
231 // might be because the renderer was deemed unresponsive and this call was | |
232 // already made by ShouldCloseTabOnUnresponsiveRenderer. In that case, it | |
233 // is ok to do nothing here. | |
234 if (pending_render_view_host_ && | |
235 pending_render_view_host_->are_navigations_suspended()) | |
236 pending_render_view_host_->SetNavigationsSuspended(false); | |
237 } else { | |
238 // Current page says to cancel. | |
239 CancelPending(); | |
240 cross_navigation_pending_ = false; | |
241 } | |
242 } else { | |
243 // Non-cross site transition means closing the entire tab. | |
244 bool proceed_to_fire_unload; | |
245 delegate_->BeforeUnloadFiredFromRenderManager(proceed, | |
246 &proceed_to_fire_unload); | |
247 | |
248 if (proceed_to_fire_unload) { | |
249 // This is not a cross-site navigation, the tab is being closed. | |
250 render_view_host_->ClosePage(false, -1, -1); | |
251 } | |
252 } | |
253 } | |
254 | |
255 void RenderViewHostManager::OnCrossSiteResponse(int new_render_process_host_id, | |
256 int new_request_id) { | |
257 // Should only see this while we have a pending renderer. | |
258 if (!cross_navigation_pending_) | |
259 return; | |
260 DCHECK(pending_render_view_host_); | |
261 | |
262 // Tell the old renderer to run its onunload handler. When it finishes, it | |
263 // will send a ClosePage_ACK to the ResourceDispatcherHost with the given | |
264 // IDs (of the pending RVH's request), allowing the pending RVH's response to | |
265 // resume. | |
266 render_view_host_->ClosePage(true, | |
267 new_render_process_host_id, new_request_id); | |
268 | |
269 // ResourceDispatcherHost has told us to run the onunload handler, which | |
270 // means it is not a download or unsafe page, and we are going to perform the | |
271 // navigation. Thus, we no longer need to remember that the RenderViewHost | |
272 // is part of a pending cross-site request. | |
273 pending_render_view_host_->SetHasPendingCrossSiteRequest(false, | |
274 new_request_id); | |
275 } | |
276 | |
277 void RenderViewHostManager::OnCrossSiteNavigationCanceled() { | |
278 DCHECK(cross_navigation_pending_); | |
279 cross_navigation_pending_ = false; | |
280 if (pending_render_view_host_) | |
281 CancelPending(); | |
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 DCHECK(new_entry); | |
294 | |
295 // Check for reasons to swap processes even if we are in a process model that | |
296 // doesn't usually swap (e.g., process-per-tab). | |
297 | |
298 // For security, we should transition between processes when one is a Web UI | |
299 // page and one isn't. If there's no cur_entry, check the current RVH's | |
300 // site, which might already be committed to a Web UI URL (such as the NTP). | |
301 const GURL& current_url = (cur_entry) ? cur_entry->url() : | |
302 render_view_host_->site_instance()->site(); | |
303 Profile* profile = delegate_->GetControllerForRenderManager().profile(); | |
304 if (WebUIFactory::UseWebUIForURL(profile, current_url)) { | |
305 // Force swap if it's not an acceptable URL for Web UI. | |
306 if (!WebUIFactory::IsURLAcceptableForWebUI(profile, new_entry->url())) | |
307 return true; | |
308 } else { | |
309 // Force swap if it's a Web UI URL. | |
310 if (WebUIFactory::UseWebUIForURL(profile, new_entry->url())) | |
311 return true; | |
312 } | |
313 | |
314 if (!cur_entry) { | |
315 // Always choose a new process when navigating to extension URLs. The | |
316 // process grouping logic will combine all of a given extension's pages | |
317 // into the same process. | |
318 if (new_entry->url().SchemeIs(chrome::kExtensionScheme)) | |
319 return true; | |
320 | |
321 return false; | |
322 } | |
323 | |
324 // We can't switch a RenderView between view source and non-view source mode | |
325 // without screwing up the session history sometimes (when navigating between | |
326 // "view-source:http://foo.com/" and "http://foo.com/", WebKit doesn't treat | |
327 // it as a new navigation). So require a view switch. | |
328 if (cur_entry->IsViewSourceMode() != new_entry->IsViewSourceMode()) | |
329 return true; | |
330 | |
331 // Also, we must switch if one is an extension and the other is not the exact | |
332 // same extension. | |
333 if (cur_entry->url().SchemeIs(chrome::kExtensionScheme) || | |
334 new_entry->url().SchemeIs(chrome::kExtensionScheme)) { | |
335 if (cur_entry->url().GetOrigin() != new_entry->url().GetOrigin()) | |
336 return true; | |
337 } | |
338 | |
339 return false; | |
340 } | |
341 | |
342 SiteInstance* RenderViewHostManager::GetSiteInstanceForEntry( | |
343 const NavigationEntry& entry, | |
344 SiteInstance* curr_instance) { | |
345 // NOTE: This is only called when ShouldTransitionCrossSite is true. | |
346 | |
347 // If the entry has an instance already, we should use it. | |
348 if (entry.site_instance()) | |
349 return entry.site_instance(); | |
350 | |
351 // (UGLY) HEURISTIC, process-per-site only: | |
352 // | |
353 // If this navigation is generated, then it probably corresponds to a search | |
354 // query. Given that search results typically lead to users navigating to | |
355 // other sites, we don't really want to use the search engine hostname to | |
356 // determine the site instance for this navigation. | |
357 // | |
358 // NOTE: This can be removed once we have a way to transition between | |
359 // RenderViews in response to a link click. | |
360 // | |
361 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kProcessPerSite) && | |
362 entry.transition_type() == PageTransition::GENERATED) | |
363 return curr_instance; | |
364 | |
365 const GURL& dest_url = entry.url(); | |
366 | |
367 // If we haven't used our SiteInstance (and thus RVH) yet, then we can use it | |
368 // for this entry. We won't commit the SiteInstance to this site until the | |
369 // navigation commits (in DidNavigate), unless the navigation entry was | |
370 // restored or it's a Web UI as described below. | |
371 if (!curr_instance->has_site()) { | |
372 // If we've already created a SiteInstance for our destination, we don't | |
373 // want to use this unused SiteInstance; use the existing one. (We don't | |
374 // do this check if the curr_instance has a site, because for now, we want | |
375 // to compare against the current URL and not the SiteInstance's site. In | |
376 // this case, there is no current URL, so comparing against the site is ok. | |
377 // See additional comments below.) | |
378 if (curr_instance->HasRelatedSiteInstance(dest_url)) { | |
379 return curr_instance->GetRelatedSiteInstance(dest_url); | |
380 } else { | |
381 // Normally the "site" on the SiteInstance is set lazily when the load | |
382 // actually commits. This is to support better process sharing in case | |
383 // the site redirects to some other site: we want to use the destination | |
384 // site in the site instance. | |
385 // | |
386 // In the case of session restore, as it loads all the pages immediately | |
387 // we need to set the site first, otherwise after a restore none of the | |
388 // pages would share renderers. | |
389 // | |
390 // For Web UI (this mostly comes up for the new tab page), the | |
391 // SiteInstance has special meaning: we never want to reassign the | |
392 // process. If you navigate to another site before the Web UI commits, | |
393 // we still want to create a new process rather than re-using the | |
394 // existing Web UI process. | |
395 if (entry.restore_type() != NavigationEntry::RESTORE_NONE || | |
396 WebUIFactory::HasWebUIScheme(dest_url)) | |
397 curr_instance->SetSite(dest_url); | |
398 return curr_instance; | |
399 } | |
400 } | |
401 | |
402 // Otherwise, only create a new SiteInstance for cross-site navigation. | |
403 | |
404 // TODO(creis): Once we intercept links and script-based navigations, we | |
405 // will be able to enforce that all entries in a SiteInstance actually have | |
406 // the same site, and it will be safe to compare the URL against the | |
407 // SiteInstance's site, as follows: | |
408 // const GURL& current_url = curr_instance->site(); | |
409 // For now, though, we're in a hybrid model where you only switch | |
410 // SiteInstances if you type in a cross-site URL. This means we have to | |
411 // compare the entry's URL to the last committed entry's URL. | |
412 NavigationController& controller = delegate_->GetControllerForRenderManager(); | |
413 NavigationEntry* curr_entry = controller.GetLastCommittedEntry(); | |
414 if (interstitial_page_) { | |
415 // The interstitial is currently the last committed entry, but we want to | |
416 // compare against the last non-interstitial entry. | |
417 curr_entry = controller.GetEntryAtOffset(-1); | |
418 } | |
419 // If there is no last non-interstitial entry (and curr_instance already | |
420 // has a site), then we must have been opened from another tab. We want | |
421 // to compare against the URL of the page that opened us, but we can't | |
422 // get to it directly. The best we can do is check against the site of | |
423 // the SiteInstance. This will be correct when we intercept links and | |
424 // script-based navigations, but for now, it could place some pages in a | |
425 // new process unnecessarily. We should only hit this case if a page tries | |
426 // to open a new tab to an interstitial-inducing URL, and then navigates | |
427 // the page to a different same-site URL. (This seems very unlikely in | |
428 // practice.) | |
429 const GURL& current_url = (curr_entry) ? curr_entry->url() : | |
430 curr_instance->site(); | |
431 Profile* profile = controller.profile(); | |
432 | |
433 if (SiteInstance::IsSameWebSite(profile, current_url, dest_url)) { | |
434 return curr_instance; | |
435 } else if (ShouldSwapProcessesForNavigation(curr_entry, &entry)) { | |
436 // When we're swapping, we need to force the site instance AND browsing | |
437 // instance to be different ones. This addresses special cases where we use | |
438 // a single BrowsingInstance for all pages of a certain type (e.g., New Tab | |
439 // Pages), keeping them in the same process. When you navigate away from | |
440 // that page, we want to explicity ignore that BrowsingInstance and group | |
441 // this page into the appropriate SiteInstance for its URL. | |
442 return SiteInstance::CreateSiteInstanceForURL(profile, dest_url); | |
443 } else { | |
444 // Start the new renderer in a new SiteInstance, but in the current | |
445 // BrowsingInstance. It is important to immediately give this new | |
446 // SiteInstance to a RenderViewHost (if it is different than our current | |
447 // SiteInstance), so that it is ref counted. This will happen in | |
448 // CreatePendingRenderView. | |
449 return curr_instance->GetRelatedSiteInstance(dest_url); | |
450 } | |
451 } | |
452 | |
453 bool RenderViewHostManager::CreatePendingRenderView( | |
454 const NavigationEntry& entry, SiteInstance* instance) { | |
455 NavigationEntry* curr_entry = | |
456 delegate_->GetControllerForRenderManager().GetLastCommittedEntry(); | |
457 if (curr_entry) { | |
458 DCHECK(!curr_entry->content_state().empty()); | |
459 // TODO(creis): Should send a message to the RenderView to let it know | |
460 // we're about to switch away, so that it sends an UpdateState message. | |
461 } | |
462 | |
463 pending_render_view_host_ = RenderViewHostFactory::Create( | |
464 instance, render_view_delegate_, MSG_ROUTING_NONE, delegate_-> | |
465 GetControllerForRenderManager().session_storage_namespace()); | |
466 NotificationService::current()->Notify( | |
467 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB, | |
468 Source<RenderViewHostManager>(this), | |
469 Details<RenderViewHost>(pending_render_view_host_)); | |
470 | |
471 bool success = InitRenderView(pending_render_view_host_, entry); | |
472 if (success) { | |
473 // Don't show the view until we get a DidNavigate from it. | |
474 pending_render_view_host_->view()->Hide(); | |
475 } else { | |
476 CancelPending(); | |
477 } | |
478 return success; | |
479 } | |
480 | |
481 bool RenderViewHostManager::InitRenderView(RenderViewHost* render_view_host, | |
482 const NavigationEntry& entry) { | |
483 // If the pending navigation is to a WebUI, tell the RenderView about any | |
484 // bindings it will need enabled. | |
485 if (pending_web_ui_.get()) | |
486 render_view_host->AllowBindings(pending_web_ui_->bindings()); | |
487 | |
488 // Tell the RenderView whether it will be used for an extension process. | |
489 Profile* profile = delegate_->GetControllerForRenderManager().profile(); | |
490 bool is_extension_process = profile->GetExtensionService() && | |
491 profile->GetExtensionService()->ExtensionBindingsAllowed(entry.url()); | |
492 render_view_host->set_is_extension_process(is_extension_process); | |
493 | |
494 return delegate_->CreateRenderViewForRenderManager(render_view_host); | |
495 } | |
496 | |
497 void RenderViewHostManager::CommitPending() { | |
498 // First check whether we're going to want to focus the location bar after | |
499 // this commit. We do this now because the navigation hasn't formally | |
500 // committed yet, so if we've already cleared |pending_web_ui_| the call chain | |
501 // this triggers won't be able to figure out what's going on. | |
502 bool will_focus_location_bar = delegate_->FocusLocationBarByDefault(); | |
503 | |
504 // Next commit the Web UI, if any. | |
505 web_ui_.swap(pending_web_ui_); | |
506 if (web_ui_.get() && pending_web_ui_.get() && !pending_render_view_host_) | |
507 web_ui_->DidBecomeActiveForReusedRenderView(); | |
508 pending_web_ui_.reset(); | |
509 | |
510 // It's possible for the pending_render_view_host_ to be NULL when we aren't | |
511 // crossing process boundaries. If so, we just needed to handle the Web UI | |
512 // committing above and we're done. | |
513 if (!pending_render_view_host_) { | |
514 if (will_focus_location_bar) | |
515 delegate_->SetFocusToLocationBar(false); | |
516 return; | |
517 } | |
518 | |
519 // Remember if the page was focused so we can focus the new renderer in | |
520 // that case. | |
521 bool focus_render_view = !will_focus_location_bar && | |
522 render_view_host_->view() && render_view_host_->view()->HasFocus(); | |
523 | |
524 // Hide the current view and prepare to destroy it. | |
525 // TODO(creis): Get the old RenderViewHost to send us an UpdateState message | |
526 // before we destroy it. | |
527 if (render_view_host_->view()) | |
528 render_view_host_->view()->Hide(); | |
529 RenderViewHost* old_render_view_host = render_view_host_; | |
530 | |
531 // Swap in the pending view and make it active. | |
532 render_view_host_ = pending_render_view_host_; | |
533 pending_render_view_host_ = NULL; | |
534 | |
535 // If the view is gone, then this RenderViewHost died while it was hidden. | |
536 // We ignored the RenderViewGone call at the time, so we should send it now | |
537 // to make sure the sad tab shows up, etc. | |
538 if (render_view_host_->view()) | |
539 render_view_host_->view()->Show(); | |
540 else | |
541 delegate_->RenderViewGoneFromRenderManager(render_view_host_); | |
542 | |
543 // Make sure the size is up to date. (Fix for bug 1079768.) | |
544 delegate_->UpdateRenderViewSizeForRenderManager(); | |
545 | |
546 if (will_focus_location_bar) | |
547 delegate_->SetFocusToLocationBar(false); | |
548 else if (focus_render_view && render_view_host_->view()) | |
549 render_view_host_->view()->Focus(); | |
550 | |
551 RenderViewHostSwitchedDetails details; | |
552 details.new_host = render_view_host_; | |
553 details.old_host = old_render_view_host; | |
554 NotificationService::current()->Notify( | |
555 NotificationType::RENDER_VIEW_HOST_CHANGED, | |
556 Source<NavigationController>(&delegate_->GetControllerForRenderManager()), | |
557 Details<RenderViewHostSwitchedDetails>(&details)); | |
558 | |
559 old_render_view_host->Shutdown(); | |
560 | |
561 // Let the task manager know that we've swapped RenderViewHosts, since it | |
562 // might need to update its process groupings. | |
563 delegate_->NotifySwappedFromRenderManager(); | |
564 } | |
565 | |
566 RenderViewHost* RenderViewHostManager::UpdateRendererStateForNavigate( | |
567 const NavigationEntry& entry) { | |
568 // If we are cross-navigating, then we want to get back to normal and navigate | |
569 // as usual. | |
570 if (cross_navigation_pending_) { | |
571 if (pending_render_view_host_) | |
572 CancelPending(); | |
573 cross_navigation_pending_ = false; | |
574 } | |
575 | |
576 // This will possibly create (set to NULL) a Web UI object for the pending | |
577 // page. We'll use this later to give the page special access. This must | |
578 // happen before the new renderer is created below so it will get bindings. | |
579 // It must also happen after the above conditional call to CancelPending(), | |
580 // otherwise CancelPending may clear the pending_web_ui_ and the page will | |
581 // not have it's bindings set appropriately. | |
582 pending_web_ui_.reset(delegate_->CreateWebUIForRenderManager(entry.url())); | |
583 | |
584 // render_view_host_ will not be deleted before the end of this method, so we | |
585 // don't have to worry about this SiteInstance's ref count dropping to zero. | |
586 SiteInstance* curr_instance = render_view_host_->site_instance(); | |
587 | |
588 // Determine if we need a new SiteInstance for this entry. | |
589 // Again, new_instance won't be deleted before the end of this method, so it | |
590 // is safe to use a normal pointer here. | |
591 SiteInstance* new_instance = curr_instance; | |
592 bool force_swap = ShouldSwapProcessesForNavigation( | |
593 delegate_->GetLastCommittedNavigationEntryForRenderManager(), | |
594 &entry); | |
595 if (ShouldTransitionCrossSite() || force_swap) | |
596 new_instance = GetSiteInstanceForEntry(entry, curr_instance); | |
597 | |
598 if (new_instance != curr_instance || force_swap) { | |
599 // New SiteInstance. | |
600 DCHECK(!cross_navigation_pending_); | |
601 | |
602 // Create a pending RVH and navigate it. | |
603 bool success = CreatePendingRenderView(entry, new_instance); | |
604 if (!success) | |
605 return NULL; | |
606 | |
607 // Check if our current RVH is live before we set up a transition. | |
608 if (!render_view_host_->IsRenderViewLive()) { | |
609 if (!cross_navigation_pending_) { | |
610 // The current RVH is not live. There's no reason to sit around with a | |
611 // sad tab or a newly created RVH while we wait for the pending RVH to | |
612 // navigate. Just switch to the pending RVH now and go back to non | |
613 // cross-navigating (Note that we don't care about on{before}unload | |
614 // handlers if the current RVH isn't live.) | |
615 CommitPending(); | |
616 return render_view_host_; | |
617 } else { | |
618 NOTREACHED(); | |
619 return render_view_host_; | |
620 } | |
621 } | |
622 // Otherwise, it's safe to treat this as a pending cross-site transition. | |
623 | |
624 // Make sure the old render view stops, in case a load is in progress. | |
625 render_view_host_->Stop(); | |
626 | |
627 // Suspend the new render view (i.e., don't let it send the cross-site | |
628 // Navigate message) until we hear back from the old renderer's | |
629 // onbeforeunload handler. If the handler returns false, we'll have to | |
630 // cancel the request. | |
631 DCHECK(!pending_render_view_host_->are_navigations_suspended()); | |
632 pending_render_view_host_->SetNavigationsSuspended(true); | |
633 | |
634 // Tell the CrossSiteRequestManager that this RVH has a pending cross-site | |
635 // request, so that ResourceDispatcherHost will know to tell us to run the | |
636 // old page's onunload handler before it sends the response. | |
637 pending_render_view_host_->SetHasPendingCrossSiteRequest(true, -1); | |
638 | |
639 // We now have a pending RVH. | |
640 DCHECK(!cross_navigation_pending_); | |
641 cross_navigation_pending_ = true; | |
642 | |
643 // Tell the old render view to run its onbeforeunload handler, since it | |
644 // doesn't otherwise know that the cross-site request is happening. This | |
645 // will trigger a call to ShouldClosePage with the reply. | |
646 render_view_host_->FirePageBeforeUnload(true); | |
647 | |
648 return pending_render_view_host_; | |
649 } else { | |
650 if (pending_web_ui_.get() && render_view_host_->IsRenderViewLive()) | |
651 pending_web_ui_->RenderViewReused(render_view_host_); | |
652 | |
653 // The renderer can exit view source mode when any error or cancellation | |
654 // happen. We must overwrite to recover the mode. | |
655 if (entry.IsViewSourceMode()) { | |
656 render_view_host_->Send( | |
657 new ViewMsg_EnableViewSourceMode(render_view_host_->routing_id())); | |
658 } | |
659 } | |
660 | |
661 // Same SiteInstance can be used. Navigate render_view_host_ if we are not | |
662 // cross navigating. | |
663 DCHECK(!cross_navigation_pending_); | |
664 return render_view_host_; | |
665 } | |
666 | |
667 void RenderViewHostManager::CancelPending() { | |
668 RenderViewHost* pending_render_view_host = pending_render_view_host_; | |
669 pending_render_view_host_ = NULL; | |
670 pending_render_view_host->Shutdown(); | |
671 | |
672 pending_web_ui_.reset(); | |
673 } | |
674 | |
675 void RenderViewHostManager::RenderViewDeleted(RenderViewHost* rvh) { | |
676 // We are doing this in order to work around and to track a crasher | |
677 // (http://crbug.com/23411) where it seems that pending_render_view_host_ is | |
678 // deleted (not sure from where) but not NULLed. | |
679 if (rvh == pending_render_view_host_) { | |
680 // If you hit this NOTREACHED, please report it in the following bug | |
681 // http://crbug.com/23411 Make sure to include what you were doing when it | |
682 // happened (navigating to a new page, closing a tab...) and if you can | |
683 // reproduce. | |
684 NOTREACHED(); | |
685 pending_render_view_host_ = NULL; | |
686 } | |
687 } | |
688 | |
689 void RenderViewHostManager::SwapInRenderViewHost(RenderViewHost* rvh) { | |
690 web_ui_.reset(); | |
691 | |
692 // Hide the current view and prepare to destroy it. | |
693 if (render_view_host_->view()) | |
694 render_view_host_->view()->Hide(); | |
695 RenderViewHost* old_render_view_host = render_view_host_; | |
696 | |
697 // Swap in the new view and make it active. | |
698 render_view_host_ = rvh; | |
699 render_view_host_->set_delegate(render_view_delegate_); | |
700 delegate_->CreateViewAndSetSizeForRVH(render_view_host_); | |
701 render_view_host_->ActivateDeferredPluginHandles(); | |
702 // If the view is gone, then this RenderViewHost died while it was hidden. | |
703 // We ignored the RenderViewGone call at the time, so we should send it now | |
704 // to make sure the sad tab shows up, etc. | |
705 if (render_view_host_->view()) { | |
706 // TODO(tburkard,cbentzel): Figure out why this hack is needed and/or | |
707 // if it can be removed. On Windows, prerendering will not work without | |
708 // doing a Hide before the Show. | |
709 render_view_host_->view()->Hide(); | |
710 render_view_host_->view()->Show(); | |
711 } | |
712 | |
713 delegate_->UpdateRenderViewSizeForRenderManager(); | |
714 | |
715 RenderViewHostSwitchedDetails details; | |
716 details.new_host = render_view_host_; | |
717 details.old_host = old_render_view_host; | |
718 NotificationService::current()->Notify( | |
719 NotificationType::RENDER_VIEW_HOST_CHANGED, | |
720 Source<NavigationController>(&delegate_->GetControllerForRenderManager()), | |
721 Details<RenderViewHostSwitchedDetails>(&details)); | |
722 | |
723 // This will cause the old RenderViewHost to delete itself. | |
724 old_render_view_host->Shutdown(); | |
725 | |
726 // Let the task manager know that we've swapped RenderViewHosts, since it | |
727 // might need to update its process groupings. | |
728 delegate_->NotifySwappedFromRenderManager(); | |
729 } | |
OLD | NEW |