This source file includes following definitions.
- has_site_
- GetId
- HasProcess
- GetProcess
- SetSite
- GetSiteURL
- HasSite
- HasRelatedSiteInstance
- GetRelatedSiteInstance
- IsRelatedSiteInstance
- HasWrongProcessForURL
- set_render_process_host_factory
- GetBrowserContext
- Create
- CreateForURL
- IsSameWebSite
- GetSiteForURL
- GetEffectiveURL
- RenderProcessHostDestroyed
- LockToOrigin
#include "content/browser/site_instance_impl.h"
#include "base/command_line.h"
#include "content/browser/browsing_instance.h"
#include "content/browser/child_process_security_policy_impl.h"
#include "content/browser/frame_host/debug_urls.h"
#include "content/browser/renderer_host/render_process_host_impl.h"
#include "content/browser/storage_partition_impl.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/render_process_host_factory.h"
#include "content/public/browser/web_ui_controller_factory.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/url_constants.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
namespace content {
const RenderProcessHostFactory*
SiteInstanceImpl::g_render_process_host_factory_ = NULL;
int32 SiteInstanceImpl::next_site_instance_id_ = 1;
SiteInstanceImpl::SiteInstanceImpl(BrowsingInstance* browsing_instance)
: id_(next_site_instance_id_++),
active_view_count_(0),
browsing_instance_(browsing_instance),
process_(NULL),
has_site_(false) {
DCHECK(browsing_instance);
}
SiteInstanceImpl::~SiteInstanceImpl() {
GetContentClient()->browser()->SiteInstanceDeleting(this);
if (process_)
process_->RemoveObserver(this);
if (has_site_)
browsing_instance_->UnregisterSiteInstance(
static_cast<SiteInstance*>(this));
}
int32 SiteInstanceImpl::GetId() {
return id_;
}
bool SiteInstanceImpl::HasProcess() const {
if (process_ != NULL)
return true;
BrowserContext* browser_context =
browsing_instance_->browser_context();
if (has_site_ &&
RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_) &&
RenderProcessHostImpl::GetProcessHostForSite(browser_context, site_)) {
return true;
}
return false;
}
RenderProcessHost* SiteInstanceImpl::GetProcess() {
if (!process_) {
BrowserContext* browser_context = browsing_instance_->browser_context();
bool use_process_per_site = has_site_ &&
RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_);
if (use_process_per_site) {
process_ = RenderProcessHostImpl::GetProcessHostForSite(browser_context,
site_);
}
if (!process_ && RenderProcessHostImpl::ShouldTryToUseExistingProcessHost(
browser_context, site_)) {
process_ = RenderProcessHostImpl::GetExistingProcessHost(browser_context,
site_);
}
if (!process_) {
if (g_render_process_host_factory_) {
process_ = g_render_process_host_factory_->CreateRenderProcessHost(
browser_context, this);
} else {
StoragePartitionImpl* partition =
static_cast<StoragePartitionImpl*>(
BrowserContext::GetStoragePartition(browser_context, this));
bool supports_browser_plugin = GetContentClient()->browser()->
SupportsBrowserPlugin(browser_context, site_);
process_ = new RenderProcessHostImpl(browser_context,
partition,
supports_browser_plugin,
site_.SchemeIs(kGuestScheme));
}
}
CHECK(process_);
process_->AddObserver(this);
if (use_process_per_site) {
RenderProcessHostImpl::RegisterProcessHostForSite(browser_context,
process_, site_);
}
GetContentClient()->browser()->SiteInstanceGotProcess(this);
if (has_site_)
LockToOrigin();
}
DCHECK(process_);
return process_;
}
void SiteInstanceImpl::SetSite(const GURL& url) {
DCHECK(!has_site_);
has_site_ = true;
BrowserContext* browser_context = browsing_instance_->browser_context();
site_ = GetSiteForURL(browser_context, url);
browsing_instance_->RegisterSiteInstance(this);
if (process_) {
LockToOrigin();
if (RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_)) {
RenderProcessHostImpl::RegisterProcessHostForSite(
browser_context, process_, site_);
}
}
}
const GURL& SiteInstanceImpl::GetSiteURL() const {
return site_;
}
bool SiteInstanceImpl::HasSite() const {
return has_site_;
}
bool SiteInstanceImpl::HasRelatedSiteInstance(const GURL& url) {
return browsing_instance_->HasSiteInstance(url);
}
SiteInstance* SiteInstanceImpl::GetRelatedSiteInstance(const GURL& url) {
return browsing_instance_->GetSiteInstanceForURL(url);
}
bool SiteInstanceImpl::IsRelatedSiteInstance(const SiteInstance* instance) {
return browsing_instance_.get() == static_cast<const SiteInstanceImpl*>(
instance)->browsing_instance_.get();
}
bool SiteInstanceImpl::HasWrongProcessForURL(const GURL& url) {
if (!HasProcess())
return false;
if (IsRendererDebugURL(url))
return false;
GURL site_url = GetSiteForURL(browsing_instance_->browser_context(), url);
return !RenderProcessHostImpl::IsSuitableHost(
GetProcess(), browsing_instance_->browser_context(), site_url);
}
void SiteInstanceImpl::set_render_process_host_factory(
const RenderProcessHostFactory* rph_factory) {
g_render_process_host_factory_ = rph_factory;
}
BrowserContext* SiteInstanceImpl::GetBrowserContext() const {
return browsing_instance_->browser_context();
}
SiteInstance* SiteInstance::Create(BrowserContext* browser_context) {
return new SiteInstanceImpl(new BrowsingInstance(browser_context));
}
SiteInstance* SiteInstance::CreateForURL(BrowserContext* browser_context,
const GURL& url) {
scoped_refptr<BrowsingInstance> instance(
new BrowsingInstance(browser_context));
return instance->GetSiteInstanceForURL(url);
}
bool SiteInstance::IsSameWebSite(BrowserContext* browser_context,
const GURL& real_url1,
const GURL& real_url2) {
GURL url1 = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url1);
GURL url2 = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url2);
if (IsRendererDebugURL(url1) || IsRendererDebugURL(url2))
return true;
if (!url1.is_valid() || !url2.is_valid())
return false;
if (url1.scheme() != url2.scheme())
return false;
return net::registry_controlled_domains::SameDomainOrHost(
url1,
url2,
net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
}
GURL SiteInstance::GetSiteForURL(BrowserContext* browser_context,
const GURL& real_url) {
if (real_url.SchemeIs(kGuestScheme))
return real_url;
GURL url = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url);
GURL site;
if (url.has_host()) {
site = url.GetOrigin();
if (site.has_port()) {
GURL::Replacements rep;
rep.ClearPort();
site = site.ReplaceComponents(rep);
}
std::string domain =
net::registry_controlled_domains::GetDomainAndRegistry(
url,
net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
if (!domain.empty()) {
GURL::Replacements rep;
rep.SetHostStr(domain);
site = site.ReplaceComponents(rep);
}
}
return site;
}
GURL SiteInstanceImpl::GetEffectiveURL(BrowserContext* browser_context,
const GURL& url) {
return GetContentClient()->browser()->
GetEffectiveURL(browser_context, url);
}
void SiteInstanceImpl::RenderProcessHostDestroyed(RenderProcessHost* host) {
DCHECK_EQ(process_, host);
process_->RemoveObserver(this);
process_ = NULL;
}
void SiteInstanceImpl::LockToOrigin() {
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
if (command_line.HasSwitch(switches::kEnableStrictSiteIsolation) ||
command_line.HasSwitch(switches::kSitePerProcess)) {
ChildProcessSecurityPolicyImpl* policy =
ChildProcessSecurityPolicyImpl::GetInstance();
policy->LockToOrigin(process_->GetID(), site_);
}
}
}