Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(168)

Side by Side Diff: content/browser/tab_contents/render_view_host_manager.cc

Issue 9146028: Define the public interface for content browser SiteInstance. This interface is implemented by th... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/tab_contents/render_view_host_manager.h" 5 #include "content/browser/tab_contents/render_view_host_manager.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "content/browser/debugger/devtools_manager_impl.h" 11 #include "content/browser/debugger/devtools_manager_impl.h"
12 #include "content/browser/renderer_host/render_view_host.h" 12 #include "content/browser/renderer_host/render_view_host.h"
13 #include "content/browser/renderer_host/render_view_host_factory.h" 13 #include "content/browser/renderer_host/render_view_host_factory.h"
14 #include "content/browser/renderer_host/render_widget_host_view.h" 14 #include "content/browser/renderer_host/render_widget_host_view.h"
15 #include "content/browser/site_instance.h" 15 #include "content/browser/site_instance_impl.h"
16 #include "content/browser/tab_contents/navigation_controller_impl.h" 16 #include "content/browser/tab_contents/navigation_controller_impl.h"
17 #include "content/browser/tab_contents/navigation_entry_impl.h" 17 #include "content/browser/tab_contents/navigation_entry_impl.h"
18 #include "content/browser/webui/web_ui_impl.h" 18 #include "content/browser/webui/web_ui_impl.h"
19 #include "content/common/view_messages.h" 19 #include "content/common/view_messages.h"
20 #include "content/public/browser/content_browser_client.h" 20 #include "content/public/browser/content_browser_client.h"
21 #include "content/public/browser/notification_service.h" 21 #include "content/public/browser/notification_service.h"
22 #include "content/public/browser/notification_types.h" 22 #include "content/public/browser/notification_types.h"
23 #include "content/public/browser/render_view_host_delegate.h" 23 #include "content/public/browser/render_view_host_delegate.h"
24 #include "content/public/browser/web_contents_view.h" 24 #include "content/public/browser/web_contents_view.h"
25 #include "content/public/browser/web_ui_controller.h" 25 #include "content/public/browser/web_ui_controller.h"
26 #include "content/public/browser/web_ui_factory.h" 26 #include "content/public/browser/web_ui_factory.h"
27 #include "content/public/common/content_switches.h" 27 #include "content/public/common/content_switches.h"
28 #include "content/public/common/url_constants.h" 28 #include "content/public/common/url_constants.h"
29 29
30 using content::NavigationController; 30 using content::NavigationController;
31 using content::NavigationEntry; 31 using content::NavigationEntry;
32 using content::NavigationEntryImpl; 32 using content::NavigationEntryImpl;
33 using content::SiteInstance;
33 34
34 RenderViewHostManager::RenderViewHostManager( 35 RenderViewHostManager::RenderViewHostManager(
35 content::RenderViewHostDelegate* render_view_delegate, 36 content::RenderViewHostDelegate* render_view_delegate,
36 Delegate* delegate) 37 Delegate* delegate)
37 : delegate_(delegate), 38 : delegate_(delegate),
38 cross_navigation_pending_(false), 39 cross_navigation_pending_(false),
39 render_view_delegate_(render_view_delegate), 40 render_view_delegate_(render_view_delegate),
40 render_view_host_(NULL), 41 render_view_host_(NULL),
41 pending_render_view_host_(NULL), 42 pending_render_view_host_(NULL),
42 interstitial_page_(NULL) { 43 interstitial_page_(NULL) {
(...skipping 16 matching lines...) Expand all
59 } 60 }
60 } 61 }
61 62
62 void RenderViewHostManager::Init(content::BrowserContext* browser_context, 63 void RenderViewHostManager::Init(content::BrowserContext* browser_context,
63 SiteInstance* site_instance, 64 SiteInstance* site_instance,
64 int routing_id) { 65 int routing_id) {
65 // Create a RenderViewHost, once we have an instance. It is important to 66 // Create a RenderViewHost, once we have an instance. It is important to
66 // immediately give this SiteInstance to a RenderViewHost so that it is 67 // immediately give this SiteInstance to a RenderViewHost so that it is
67 // ref counted. 68 // ref counted.
68 if (!site_instance) 69 if (!site_instance)
69 site_instance = SiteInstance::CreateSiteInstance(browser_context); 70 site_instance = SiteInstance::Create(browser_context);
70 render_view_host_ = RenderViewHostFactory::Create( 71 render_view_host_ = RenderViewHostFactory::Create(
71 site_instance, render_view_delegate_, routing_id, delegate_-> 72 site_instance, render_view_delegate_, routing_id, delegate_->
72 GetControllerForRenderManager().GetSessionStorageNamespace()); 73 GetControllerForRenderManager().GetSessionStorageNamespace());
73 74
74 // Keep track of renderer processes as they start to shut down. 75 // Keep track of renderer processes as they start to shut down.
75 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSING, 76 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSING,
76 content::NotificationService::AllSources()); 77 content::NotificationService::AllSources());
77 } 78 }
78 79
79 RenderWidgetHostView* RenderViewHostManager::GetRenderWidgetHostView() const { 80 RenderWidgetHostView* RenderViewHostManager::GetRenderWidgetHostView() const {
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 const NavigationEntryImpl* new_entry) const { 339 const NavigationEntryImpl* new_entry) const {
339 DCHECK(new_entry); 340 DCHECK(new_entry);
340 341
341 // Check for reasons to swap processes even if we are in a process model that 342 // Check for reasons to swap processes even if we are in a process model that
342 // doesn't usually swap (e.g., process-per-tab). 343 // doesn't usually swap (e.g., process-per-tab).
343 344
344 // For security, we should transition between processes when one is a Web UI 345 // For security, we should transition between processes when one is a Web UI
345 // page and one isn't. If there's no cur_entry, check the current RVH's 346 // page and one isn't. If there's no cur_entry, check the current RVH's
346 // site, which might already be committed to a Web UI URL (such as the NTP). 347 // site, which might already be committed to a Web UI URL (such as the NTP).
347 const GURL& current_url = (cur_entry) ? cur_entry->GetURL() : 348 const GURL& current_url = (cur_entry) ? cur_entry->GetURL() :
348 render_view_host_->site_instance()->site(); 349 render_view_host_->site_instance()->GetSite();
349 content::BrowserContext* browser_context = 350 content::BrowserContext* browser_context =
350 delegate_->GetControllerForRenderManager().GetBrowserContext(); 351 delegate_->GetControllerForRenderManager().GetBrowserContext();
351 const content::WebUIFactory* web_ui_factory = 352 const content::WebUIFactory* web_ui_factory =
352 content::GetContentClient()->browser()->GetWebUIFactory(); 353 content::GetContentClient()->browser()->GetWebUIFactory();
353 if (web_ui_factory->UseWebUIForURL(browser_context, current_url)) { 354 if (web_ui_factory->UseWebUIForURL(browser_context, current_url)) {
354 // Force swap if it's not an acceptable URL for Web UI. 355 // Force swap if it's not an acceptable URL for Web UI.
355 if (!web_ui_factory->IsURLAcceptableForWebUI(browser_context, 356 if (!web_ui_factory->IsURLAcceptableForWebUI(browser_context,
356 new_entry->GetURL())) 357 new_entry->GetURL()))
357 return true; 358 return true;
358 } else { 359 } else {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 // RenderViews in response to a link click. 405 // RenderViews in response to a link click.
405 // 406 //
406 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kProcessPerSite) && 407 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kProcessPerSite) &&
407 entry.GetTransitionType() == content::PAGE_TRANSITION_GENERATED) 408 entry.GetTransitionType() == content::PAGE_TRANSITION_GENERATED)
408 return curr_instance; 409 return curr_instance;
409 410
410 // If we haven't used our SiteInstance (and thus RVH) yet, then we can use it 411 // If we haven't used our SiteInstance (and thus RVH) yet, then we can use it
411 // for this entry. We won't commit the SiteInstance to this site until the 412 // for this entry. We won't commit the SiteInstance to this site until the
412 // navigation commits (in DidNavigate), unless the navigation entry was 413 // navigation commits (in DidNavigate), unless the navigation entry was
413 // restored or it's a Web UI as described below. 414 // restored or it's a Web UI as described below.
414 if (!curr_instance->has_site()) { 415 if (!static_cast<SiteInstanceImpl*>(curr_instance)->HasSite()) {
jam 2012/01/25 02:02:17 nit: to improve readability you might want to cast
ananta 2012/01/25 02:16:29 Done.
415 // If we've already created a SiteInstance for our destination, we don't 416 // If we've already created a SiteInstance for our destination, we don't
416 // want to use this unused SiteInstance; use the existing one. (We don't 417 // want to use this unused SiteInstance; use the existing one. (We don't
417 // do this check if the curr_instance has a site, because for now, we want 418 // do this check if the curr_instance has a site, because for now, we want
418 // to compare against the current URL and not the SiteInstance's site. In 419 // to compare against the current URL and not the SiteInstance's site. In
419 // this case, there is no current URL, so comparing against the site is ok. 420 // this case, there is no current URL, so comparing against the site is ok.
420 // See additional comments below.) 421 // See additional comments below.)
421 if (curr_instance->HasRelatedSiteInstance(dest_url)) 422 if (static_cast<SiteInstanceImpl*>(curr_instance)->HasRelatedSiteInstance(
423 dest_url))
422 return curr_instance->GetRelatedSiteInstance(dest_url); 424 return curr_instance->GetRelatedSiteInstance(dest_url);
423 425
424 // For extensions, Web UI URLs (such as the new tab page), and apps we do 426 // For extensions, Web UI URLs (such as the new tab page), and apps we do
425 // not want to use the curr_instance if it has no site, since it will have a 427 // not want to use the curr_instance if it has no site, since it will have a
426 // RenderProcessHost of PRIV_NORMAL. Create a new SiteInstance for this 428 // RenderProcessHost of PRIV_NORMAL. Create a new SiteInstance for this
427 // URL instead (with the correct process type). 429 // URL instead (with the correct process type).
428 if (curr_instance->HasWrongProcessForURL(dest_url)) 430 if (static_cast<SiteInstanceImpl*>(curr_instance)->HasWrongProcessForURL(
431 dest_url))
429 return curr_instance->GetRelatedSiteInstance(dest_url); 432 return curr_instance->GetRelatedSiteInstance(dest_url);
430 433
431 // Normally the "site" on the SiteInstance is set lazily when the load 434 // Normally the "site" on the SiteInstance is set lazily when the load
432 // actually commits. This is to support better process sharing in case 435 // actually commits. This is to support better process sharing in case
433 // the site redirects to some other site: we want to use the destination 436 // the site redirects to some other site: we want to use the destination
434 // site in the site instance. 437 // site in the site instance.
435 // 438 //
436 // In the case of session restore, as it loads all the pages immediately 439 // In the case of session restore, as it loads all the pages immediately
437 // we need to set the site first, otherwise after a restore none of the 440 // we need to set the site first, otherwise after a restore none of the
438 // pages would share renderers in process-per-site. 441 // pages would share renderers in process-per-site.
439 if (entry.restore_type() != NavigationEntryImpl::RESTORE_NONE) 442 if (entry.restore_type() != NavigationEntryImpl::RESTORE_NONE)
440 curr_instance->SetSite(dest_url); 443 static_cast<SiteInstanceImpl*>(curr_instance)->SetSite(dest_url);
441 444
442 return curr_instance; 445 return curr_instance;
443 } 446 }
444 447
445 // Otherwise, only create a new SiteInstance for cross-site navigation. 448 // Otherwise, only create a new SiteInstance for cross-site navigation.
446 449
447 // TODO(creis): Once we intercept links and script-based navigations, we 450 // TODO(creis): Once we intercept links and script-based navigations, we
448 // will be able to enforce that all entries in a SiteInstance actually have 451 // will be able to enforce that all entries in a SiteInstance actually have
449 // the same site, and it will be safe to compare the URL against the 452 // the same site, and it will be safe to compare the URL against the
450 // SiteInstance's site, as follows: 453 // SiteInstance's site, as follows:
(...skipping 11 matching lines...) Expand all
462 // has a site), then we must have been opened from another tab. We want 465 // has a site), then we must have been opened from another tab. We want
463 // to compare against the URL of the page that opened us, but we can't 466 // to compare against the URL of the page that opened us, but we can't
464 // get to it directly. The best we can do is check against the site of 467 // get to it directly. The best we can do is check against the site of
465 // the SiteInstance. This will be correct when we intercept links and 468 // the SiteInstance. This will be correct when we intercept links and
466 // script-based navigations, but for now, it could place some pages in a 469 // script-based navigations, but for now, it could place some pages in a
467 // new process unnecessarily. We should only hit this case if a page tries 470 // new process unnecessarily. We should only hit this case if a page tries
468 // to open a new tab to an interstitial-inducing URL, and then navigates 471 // to open a new tab to an interstitial-inducing URL, and then navigates
469 // the page to a different same-site URL. (This seems very unlikely in 472 // the page to a different same-site URL. (This seems very unlikely in
470 // practice.) 473 // practice.)
471 const GURL& current_url = (curr_entry) ? curr_entry->GetURL() : 474 const GURL& current_url = (curr_entry) ? curr_entry->GetURL() :
472 curr_instance->site(); 475 curr_instance->GetSite();
473 476
474 // Use the current SiteInstance for same site navigations, as long as the 477 // Use the current SiteInstance for same site navigations, as long as the
475 // process type is correct. (The URL may have been installed as an app since 478 // process type is correct. (The URL may have been installed as an app since
476 // the last time we visited it.) 479 // the last time we visited it.)
477 if (SiteInstance::IsSameWebSite(browser_context, current_url, dest_url) && 480 if (SiteInstance::IsSameWebSite(browser_context, current_url, dest_url) &&
478 !curr_instance->HasWrongProcessForURL(dest_url)) { 481 !static_cast<SiteInstanceImpl*>(curr_instance)->HasWrongProcessForURL(
482 dest_url)) {
479 return curr_instance; 483 return curr_instance;
480 } else if (ShouldSwapProcessesForNavigation(curr_entry, &entry)) { 484 } else if (ShouldSwapProcessesForNavigation(curr_entry, &entry)) {
481 // When we're swapping, we need to force the site instance AND browsing 485 // When we're swapping, we need to force the site instance AND browsing
482 // instance to be different ones. This addresses special cases where we use 486 // instance to be different ones. This addresses special cases where we use
483 // a single BrowsingInstance for all pages of a certain type (e.g., New Tab 487 // a single BrowsingInstance for all pages of a certain type (e.g., New Tab
484 // Pages), keeping them in the same process. When you navigate away from 488 // Pages), keeping them in the same process. When you navigate away from
485 // that page, we want to explicity ignore that BrowsingInstance and group 489 // that page, we want to explicity ignore that BrowsingInstance and group
486 // this page into the appropriate SiteInstance for its URL. 490 // this page into the appropriate SiteInstance for its URL.
487 return SiteInstance::CreateSiteInstanceForURL(browser_context, dest_url); 491 return SiteInstance::CreateForURL(browser_context, dest_url);
488 } else { 492 } else {
489 // Start the new renderer in a new SiteInstance, but in the current 493 // Start the new renderer in a new SiteInstance, but in the current
490 // BrowsingInstance. It is important to immediately give this new 494 // BrowsingInstance. It is important to immediately give this new
491 // SiteInstance to a RenderViewHost (if it is different than our current 495 // SiteInstance to a RenderViewHost (if it is different than our current
492 // SiteInstance), so that it is ref counted. This will happen in 496 // SiteInstance), so that it is ref counted. This will happen in
493 // CreatePendingRenderView. 497 // CreatePendingRenderView.
494 return curr_instance->GetRelatedSiteInstance(dest_url); 498 return curr_instance->GetRelatedSiteInstance(dest_url);
495 } 499 }
496 } 500 }
497 501
498 bool RenderViewHostManager::CreatePendingRenderView( 502 bool RenderViewHostManager::CreatePendingRenderView(
499 const NavigationEntryImpl& entry, SiteInstance* instance) { 503 const NavigationEntryImpl& entry, SiteInstance* instance) {
500 NavigationEntry* curr_entry = 504 NavigationEntry* curr_entry =
501 delegate_->GetControllerForRenderManager().GetLastCommittedEntry(); 505 delegate_->GetControllerForRenderManager().GetLastCommittedEntry();
502 if (curr_entry) { 506 if (curr_entry) {
503 DCHECK(!curr_entry->GetContentState().empty()); 507 DCHECK(!curr_entry->GetContentState().empty());
504 // TODO(creis): Should send a message to the RenderView to let it know 508 // TODO(creis): Should send a message to the RenderView to let it know
505 // we're about to switch away, so that it sends an UpdateState message. 509 // we're about to switch away, so that it sends an UpdateState message.
506 } 510 }
507 511
508 // Check if we've already created an RVH for this SiteInstance. 512 // Check if we've already created an RVH for this SiteInstance.
509 CHECK(instance); 513 CHECK(instance);
510 RenderViewHostMap::iterator iter = 514 RenderViewHostMap::iterator iter =
511 swapped_out_hosts_.find(instance->id()); 515 swapped_out_hosts_.find(instance->GetId());
512 if (iter != swapped_out_hosts_.end()) { 516 if (iter != swapped_out_hosts_.end()) {
513 // Re-use the existing RenderViewHost, which has already been initialized. 517 // Re-use the existing RenderViewHost, which has already been initialized.
514 // We'll remove it from the list of swapped out hosts if it commits. 518 // We'll remove it from the list of swapped out hosts if it commits.
515 pending_render_view_host_ = iter->second; 519 pending_render_view_host_ = iter->second;
516 520
517 // Prevent the process from exiting while we're trying to use it. 521 // Prevent the process from exiting while we're trying to use it.
518 pending_render_view_host_->process()->AddPendingView(); 522 pending_render_view_host_->process()->AddPendingView();
519 523
520 return true; 524 return true;
521 } 525 }
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
606 610
607 std::pair<RenderViewHost*, RenderViewHost*> details = 611 std::pair<RenderViewHost*, RenderViewHost*> details =
608 std::make_pair(old_render_view_host, render_view_host_); 612 std::make_pair(old_render_view_host, render_view_host_);
609 content::NotificationService::current()->Notify( 613 content::NotificationService::current()->Notify(
610 content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED, 614 content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED,
611 content::Source<NavigationController>( 615 content::Source<NavigationController>(
612 &delegate_->GetControllerForRenderManager()), 616 &delegate_->GetControllerForRenderManager()),
613 content::Details<std::pair<RenderViewHost*, RenderViewHost*> >(&details)); 617 content::Details<std::pair<RenderViewHost*, RenderViewHost*> >(&details));
614 618
615 // If the pending view was on the swapped out list, we can remove it. 619 // If the pending view was on the swapped out list, we can remove it.
616 swapped_out_hosts_.erase(render_view_host_->site_instance()->id()); 620 swapped_out_hosts_.erase(render_view_host_->site_instance()->GetId());
617 621
618 // If the old RVH is live, we are swapping it out and should keep track of it 622 // If the old RVH is live, we are swapping it out and should keep track of it
619 // in case we navigate back to it. 623 // in case we navigate back to it.
620 if (old_render_view_host->IsRenderViewLive()) { 624 if (old_render_view_host->IsRenderViewLive()) {
621 DCHECK(old_render_view_host->is_swapped_out()); 625 DCHECK(old_render_view_host->is_swapped_out());
622 // Temp fix for http://crbug.com/90867 until we do a better cleanup to make 626 // Temp fix for http://crbug.com/90867 until we do a better cleanup to make
623 // sure we don't get different rvh instances for the same site instance 627 // sure we don't get different rvh instances for the same site instance
624 // in the same rvhmgr. 628 // in the same rvhmgr.
625 // TODO(creis): Clean this up. 629 // TODO(creis): Clean this up.
626 int32 old_site_instance_id = old_render_view_host->site_instance()->id(); 630 int32 old_site_instance_id = old_render_view_host->site_instance()->GetId();
627 RenderViewHostMap::iterator iter = 631 RenderViewHostMap::iterator iter =
628 swapped_out_hosts_.find(old_site_instance_id); 632 swapped_out_hosts_.find(old_site_instance_id);
629 if (iter != swapped_out_hosts_.end() && 633 if (iter != swapped_out_hosts_.end() &&
630 iter->second != old_render_view_host) { 634 iter->second != old_render_view_host) {
631 // Shutdown the RVH that will be replaced in the map to avoid a leak. 635 // Shutdown the RVH that will be replaced in the map to avoid a leak.
632 iter->second->Shutdown(); 636 iter->second->Shutdown();
633 } 637 }
634 swapped_out_hosts_[old_site_instance_id] = old_render_view_host; 638 swapped_out_hosts_[old_site_instance_id] = old_render_view_host;
635 } else { 639 } else {
636 old_render_view_host->Shutdown(); 640 old_render_view_host->Shutdown();
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
808 swapped_out_hosts_.erase(iter); 812 swapped_out_hosts_.erase(iter);
809 break; 813 break;
810 } 814 }
811 } 815 }
812 } 816 }
813 817
814 bool RenderViewHostManager::IsSwappedOut(RenderViewHost* rvh) { 818 bool RenderViewHostManager::IsSwappedOut(RenderViewHost* rvh) {
815 if (!rvh->site_instance()) 819 if (!rvh->site_instance())
816 return false; 820 return false;
817 821
818 return swapped_out_hosts_.find(rvh->site_instance()->id()) != 822 return swapped_out_hosts_.find(rvh->site_instance()->GetId()) !=
819 swapped_out_hosts_.end(); 823 swapped_out_hosts_.end();
820 } 824 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698