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
3 // found in the LICENSE file.
5 #include "content/browser/site_instance_impl.h"
7 #include "base/command_line.h"
8 #include "content/browser/browsing_instance.h"
9 #include "content/browser/child_process_security_policy_impl.h"
10 #include "content/browser/frame_host/debug_urls.h"
11 #include "content/browser/renderer_host/render_process_host_impl.h"
12 #include "content/browser/storage_partition_impl.h"
13 #include "content/public/browser/content_browser_client.h"
14 #include "content/public/browser/render_process_host_factory.h"
15 #include "content/public/browser/web_ui_controller_factory.h"
16 #include "content/public/common/content_switches.h"
17 #include "content/public/common/url_constants.h"
18 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
22 const RenderProcessHostFactory
*
23 SiteInstanceImpl::g_render_process_host_factory_
= NULL
;
24 int32
SiteInstanceImpl::next_site_instance_id_
= 1;
26 SiteInstanceImpl::SiteInstanceImpl(BrowsingInstance
* browsing_instance
)
27 : id_(next_site_instance_id_
++),
28 active_frame_count_(0),
29 browsing_instance_(browsing_instance
),
32 DCHECK(browsing_instance
);
35 SiteInstanceImpl::~SiteInstanceImpl() {
36 GetContentClient()->browser()->SiteInstanceDeleting(this);
39 process_
->RemoveObserver(this);
41 // Now that no one is referencing us, we can safely remove ourselves from
42 // the BrowsingInstance. Any future visits to a page from this site
43 // (within the same BrowsingInstance) can safely create a new SiteInstance.
45 browsing_instance_
->UnregisterSiteInstance(
46 static_cast<SiteInstance
*>(this));
49 int32
SiteInstanceImpl::GetId() {
53 bool SiteInstanceImpl::HasProcess() const {
57 // If we would use process-per-site for this site, also check if there is an
58 // existing process that we would use if GetProcess() were called.
59 BrowserContext
* browser_context
=
60 browsing_instance_
->browser_context();
62 RenderProcessHost::ShouldUseProcessPerSite(browser_context
, site_
) &&
63 RenderProcessHostImpl::GetProcessHostForSite(browser_context
, site_
)) {
70 RenderProcessHost
* SiteInstanceImpl::GetProcess() {
71 // TODO(erikkay) It would be nice to ensure that the renderer type had been
72 // properly set before we get here. The default tab creation case winds up
73 // with no site set at this point, so it will default to TYPE_NORMAL. This
74 // may not be correct, so we'll wind up potentially creating a process that
75 // we then throw away, or worse sharing a process with the wrong process type.
76 // See crbug.com/43448.
78 // Create a new process if ours went away or was reused.
80 BrowserContext
* browser_context
= browsing_instance_
->browser_context();
82 // If we should use process-per-site mode (either in general or for the
83 // given site), then look for an existing RenderProcessHost for the site.
84 bool use_process_per_site
= has_site_
&&
85 RenderProcessHost::ShouldUseProcessPerSite(browser_context
, site_
);
86 if (use_process_per_site
) {
87 process_
= RenderProcessHostImpl::GetProcessHostForSite(browser_context
,
91 // If not (or if none found), see if we should reuse an existing process.
92 if (!process_
&& RenderProcessHostImpl::ShouldTryToUseExistingProcessHost(
93 browser_context
, site_
)) {
94 process_
= RenderProcessHostImpl::GetExistingProcessHost(browser_context
,
98 // Otherwise (or if that fails), create a new one.
100 if (g_render_process_host_factory_
) {
101 process_
= g_render_process_host_factory_
->CreateRenderProcessHost(
102 browser_context
, this);
104 StoragePartitionImpl
* partition
=
105 static_cast<StoragePartitionImpl
*>(
106 BrowserContext::GetStoragePartition(browser_context
, this));
107 process_
= new RenderProcessHostImpl(browser_context
,
109 site_
.SchemeIs(kGuestScheme
));
113 process_
->AddObserver(this);
115 // If we are using process-per-site, we need to register this process
116 // for the current site so that we can find it again. (If no site is set
117 // at this time, we will register it in SetSite().)
118 if (use_process_per_site
) {
119 RenderProcessHostImpl::RegisterProcessHostForSite(browser_context
,
123 TRACE_EVENT2("navigation", "SiteInstanceImpl::GetProcess",
124 "site id", id_
, "process id", process_
->GetID());
125 GetContentClient()->browser()->SiteInstanceGotProcess(this);
135 void SiteInstanceImpl::SetSite(const GURL
& url
) {
136 TRACE_EVENT2("navigation", "SiteInstanceImpl::SetSite",
137 "site id", id_
, "url", url
.possibly_invalid_spec());
138 // A SiteInstance's site should not change.
139 // TODO(creis): When following links or script navigations, we can currently
140 // render pages from other sites in this SiteInstance. This will eventually
141 // be fixed, but until then, we should still not set the site of a
142 // SiteInstance more than once.
145 // Remember that this SiteInstance has been used to load a URL, even if the
148 BrowserContext
* browser_context
= browsing_instance_
->browser_context();
149 site_
= GetSiteForURL(browser_context
, url
);
151 // Now that we have a site, register it with the BrowsingInstance. This
152 // ensures that we won't create another SiteInstance for this site within
153 // the same BrowsingInstance, because all same-site pages within a
154 // BrowsingInstance can script each other.
155 browsing_instance_
->RegisterSiteInstance(this);
160 // Ensure the process is registered for this site if necessary.
161 if (RenderProcessHost::ShouldUseProcessPerSite(browser_context
, site_
)) {
162 RenderProcessHostImpl::RegisterProcessHostForSite(
163 browser_context
, process_
, site_
);
168 const GURL
& SiteInstanceImpl::GetSiteURL() const {
172 bool SiteInstanceImpl::HasSite() const {
176 bool SiteInstanceImpl::HasRelatedSiteInstance(const GURL
& url
) {
177 return browsing_instance_
->HasSiteInstance(url
);
180 SiteInstance
* SiteInstanceImpl::GetRelatedSiteInstance(const GURL
& url
) {
181 return browsing_instance_
->GetSiteInstanceForURL(url
);
184 bool SiteInstanceImpl::IsRelatedSiteInstance(const SiteInstance
* instance
) {
185 return browsing_instance_
.get() == static_cast<const SiteInstanceImpl
*>(
186 instance
)->browsing_instance_
.get();
189 size_t SiteInstanceImpl::GetRelatedActiveContentsCount() {
190 return browsing_instance_
->active_contents_count();
193 bool SiteInstanceImpl::HasWrongProcessForURL(const GURL
& url
) {
194 // Having no process isn't a problem, since we'll assign it correctly.
195 // Note that HasProcess() may return true if process_ is null, in
196 // process-per-site cases where there's an existing process available.
197 // We want to use such a process in the IsSuitableHost check, so we
198 // may end up assigning process_ in the GetProcess() call below.
202 // If the URL to navigate to can be associated with any site instance,
203 // we want to keep it in the same process.
204 if (IsRendererDebugURL(url
))
207 // If the site URL is an extension (e.g., for hosted apps or WebUI) but the
208 // process is not (or vice versa), make sure we notice and fix it.
209 GURL site_url
= GetSiteForURL(browsing_instance_
->browser_context(), url
);
210 return !RenderProcessHostImpl::IsSuitableHost(
211 GetProcess(), browsing_instance_
->browser_context(), site_url
);
214 void SiteInstanceImpl::IncrementRelatedActiveContentsCount() {
215 browsing_instance_
->increment_active_contents_count();
218 void SiteInstanceImpl::DecrementRelatedActiveContentsCount() {
219 browsing_instance_
->decrement_active_contents_count();
222 void SiteInstanceImpl::set_render_process_host_factory(
223 const RenderProcessHostFactory
* rph_factory
) {
224 g_render_process_host_factory_
= rph_factory
;
227 BrowserContext
* SiteInstanceImpl::GetBrowserContext() const {
228 return browsing_instance_
->browser_context();
232 SiteInstance
* SiteInstance::Create(BrowserContext
* browser_context
) {
233 return new SiteInstanceImpl(new BrowsingInstance(browser_context
));
237 SiteInstance
* SiteInstance::CreateForURL(BrowserContext
* browser_context
,
239 // This BrowsingInstance may be deleted if it returns an existing
241 scoped_refptr
<BrowsingInstance
> instance(
242 new BrowsingInstance(browser_context
));
243 return instance
->GetSiteInstanceForURL(url
);
247 bool SiteInstance::IsSameWebSite(BrowserContext
* browser_context
,
248 const GURL
& real_src_url
,
249 const GURL
& real_dest_url
) {
250 GURL src_url
= SiteInstanceImpl::GetEffectiveURL(browser_context
,
252 GURL dest_url
= SiteInstanceImpl::GetEffectiveURL(browser_context
,
255 // We infer web site boundaries based on the registered domain name of the
256 // top-level page and the scheme. We do not pay attention to the port if
257 // one is present, because pages served from different ports can still
258 // access each other if they change their document.domain variable.
260 // Some special URLs will match the site instance of any other URL. This is
261 // done before checking both of them for validity, since we want these URLs
262 // to have the same site instance as even an invalid one.
263 if (IsRendererDebugURL(src_url
) || IsRendererDebugURL(dest_url
))
266 // If either URL is invalid, they aren't part of the same site.
267 if (!src_url
.is_valid() || !dest_url
.is_valid())
270 // If the destination url is just a blank page, we treat them as part of the
272 GURL
blank_page(url::kAboutBlankURL
);
273 if (dest_url
== blank_page
)
276 // If the schemes differ, they aren't part of the same site.
277 if (src_url
.scheme() != dest_url
.scheme())
280 return net::registry_controlled_domains::SameDomainOrHost(
283 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES
);
287 GURL
SiteInstance::GetSiteForURL(BrowserContext
* browser_context
,
288 const GURL
& real_url
) {
289 // TODO(fsamuel, creis): For some reason appID is not recognized as a host.
290 if (real_url
.SchemeIs(kGuestScheme
))
293 GURL url
= SiteInstanceImpl::GetEffectiveURL(browser_context
, real_url
);
295 // URLs with no host should have an empty site.
298 // TODO(creis): For many protocols, we should just treat the scheme as the
299 // site, since there is no host. e.g., file:, about:, chrome:
301 // If the url has a host, then determine the site.
302 if (url
.has_host()) {
303 // Only keep the scheme and registered domain as given by GetOrigin. This
304 // may also include a port, which we need to drop.
305 site
= url
.GetOrigin();
307 // Remove port, if any.
308 if (site
.has_port()) {
309 GURL::Replacements rep
;
311 site
= site
.ReplaceComponents(rep
);
314 // If this URL has a registered domain, we only want to remember that part.
316 net::registry_controlled_domains::GetDomainAndRegistry(
318 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES
);
319 if (!domain
.empty()) {
320 GURL::Replacements rep
;
321 rep
.SetHostStr(domain
);
322 site
= site
.ReplaceComponents(rep
);
329 GURL
SiteInstanceImpl::GetEffectiveURL(BrowserContext
* browser_context
,
331 return GetContentClient()->browser()->
332 GetEffectiveURL(browser_context
, url
);
335 void SiteInstanceImpl::RenderProcessHostDestroyed(RenderProcessHost
* host
) {
336 DCHECK_EQ(process_
, host
);
337 process_
->RemoveObserver(this);
341 void SiteInstanceImpl::LockToOrigin() {
342 // We currently only restrict this process to a particular site if the
343 // --enable-strict-site-isolation or --site-per-process flags are present.
344 const base::CommandLine
& command_line
=
345 *base::CommandLine::ForCurrentProcess();
346 if (command_line
.HasSwitch(switches::kEnableStrictSiteIsolation
) ||
347 command_line
.HasSwitch(switches::kSitePerProcess
)) {
348 ChildProcessSecurityPolicyImpl
* policy
=
349 ChildProcessSecurityPolicyImpl::GetInstance();
350 policy
->LockToOrigin(process_
->GetID(), site_
);
354 } // namespace content