blob: 46751c2963e7a5666c1ab9dc82798e2d5085db0f [file] [log] [blame]
[email protected]144a8102012-01-14 01:05:311// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]b6583592012-01-25 19:52:335#include "content/browser/site_instance_impl.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]39365212011-02-24 01:01:007#include "content/browser/browsing_instance.h"
[email protected]b9535422012-02-09 01:47:598#include "content/browser/child_process_security_policy_impl.h"
[email protected]c02f1ba2014-02-03 06:53:539#include "content/browser/frame_host/debug_urls.h"
[email protected]f3b1a082011-11-18 00:34:3010#include "content/browser/renderer_host/render_process_host_impl.h"
[email protected]4c3a23582012-08-18 08:54:3411#include "content/browser/storage_partition_impl.h"
nickd30fd962015-07-27 21:51:0812#include "content/common/site_isolation_policy.h"
[email protected]87f3c082011-10-19 18:07:4413#include "content/public/browser/content_browser_client.h"
[email protected]f3b1a082011-11-18 00:34:3014#include "content/public/browser/render_process_host_factory.h"
[email protected]41fb79a52012-06-29 16:34:3315#include "content/public/browser/web_ui_controller_factory.h"
[email protected]a1d29162011-10-14 17:14:0316#include "content/public/common/url_constants.h"
[email protected]be28b5f42012-07-20 11:31:2517#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
initial.commit09911bf2008-07-26 23:55:2918
[email protected]46488322012-10-30 03:22:2019namespace content {
[email protected]b6583592012-01-25 19:52:3320
[email protected]3059caa2013-06-06 03:48:4121const RenderProcessHostFactory*
22 SiteInstanceImpl::g_render_process_host_factory_ = NULL;
[email protected]b6583592012-01-25 19:52:3323int32 SiteInstanceImpl::next_site_instance_id_ = 1;
[email protected]992db4c2011-05-12 15:37:1524
[email protected]b6583592012-01-25 19:52:3325SiteInstanceImpl::SiteInstanceImpl(BrowsingInstance* browsing_instance)
[email protected]992db4c2011-05-12 15:37:1526 : id_(next_site_instance_id_++),
creiscce56cd2014-09-29 22:45:2227 active_frame_count_(0),
[email protected]992db4c2011-05-12 15:37:1528 browsing_instance_(browsing_instance),
[email protected]4566f132009-03-12 01:55:1329 process_(NULL),
[email protected]4566f132009-03-12 01:55:1330 has_site_(false) {
31 DCHECK(browsing_instance);
[email protected]4566f132009-03-12 01:55:1332}
33
[email protected]b6583592012-01-25 19:52:3334SiteInstanceImpl::~SiteInstanceImpl() {
[email protected]46488322012-10-30 03:22:2035 GetContentClient()->browser()->SiteInstanceDeleting(this);
[email protected]056ad2a2011-07-12 02:13:5536
[email protected]40bfaf32013-11-19 01:35:4337 if (process_)
38 process_->RemoveObserver(this);
39
initial.commit09911bf2008-07-26 23:55:2940 // Now that no one is referencing us, we can safely remove ourselves from
41 // the BrowsingInstance. Any future visits to a page from this site
42 // (within the same BrowsingInstance) can safely create a new SiteInstance.
43 if (has_site_)
[email protected]b6583592012-01-25 19:52:3344 browsing_instance_->UnregisterSiteInstance(
45 static_cast<SiteInstance*>(this));
initial.commit09911bf2008-07-26 23:55:2946}
47
[email protected]b6583592012-01-25 19:52:3348int32 SiteInstanceImpl::GetId() {
49 return id_;
50}
51
52bool SiteInstanceImpl::HasProcess() const {
[email protected]41fb79a52012-06-29 16:34:3353 if (process_ != NULL)
54 return true;
55
56 // If we would use process-per-site for this site, also check if there is an
57 // existing process that we would use if GetProcess() were called.
[email protected]46488322012-10-30 03:22:2058 BrowserContext* browser_context =
[email protected]41fb79a52012-06-29 16:34:3359 browsing_instance_->browser_context();
60 if (has_site_ &&
[email protected]1ae93fb12013-06-14 03:38:5661 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_) &&
[email protected]41fb79a52012-06-29 16:34:3362 RenderProcessHostImpl::GetProcessHostForSite(browser_context, site_)) {
63 return true;
64 }
65
66 return false;
[email protected]5ab79b02010-04-26 16:47:1167}
68
[email protected]41fb79a52012-06-29 16:34:3369RenderProcessHost* SiteInstanceImpl::GetProcess() {
[email protected]08c8ec7e2010-07-11 17:14:4870 // TODO(erikkay) It would be nice to ensure that the renderer type had been
71 // properly set before we get here. The default tab creation case winds up
72 // with no site set at this point, so it will default to TYPE_NORMAL. This
73 // may not be correct, so we'll wind up potentially creating a process that
74 // we then throw away, or worse sharing a process with the wrong process type.
75 // See crbug.com/43448.
76
initial.commit09911bf2008-07-26 23:55:2977 // Create a new process if ours went away or was reused.
[email protected]4566f132009-03-12 01:55:1378 if (!process_) {
[email protected]4c3a23582012-08-18 08:54:3479 BrowserContext* browser_context = browsing_instance_->browser_context();
[email protected]41fb79a52012-06-29 16:34:3380
81 // If we should use process-per-site mode (either in general or for the
82 // given site), then look for an existing RenderProcessHost for the site.
83 bool use_process_per_site = has_site_ &&
[email protected]1ae93fb12013-06-14 03:38:5684 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_);
[email protected]41fb79a52012-06-29 16:34:3385 if (use_process_per_site) {
86 process_ = RenderProcessHostImpl::GetProcessHostForSite(browser_context,
87 site_);
88 }
89
90 // If not (or if none found), see if we should reuse an existing process.
91 if (!process_ && RenderProcessHostImpl::ShouldTryToUseExistingProcessHost(
92 browser_context, site_)) {
93 process_ = RenderProcessHostImpl::GetExistingProcessHost(browser_context,
94 site_);
95 }
initial.commit09911bf2008-07-26 23:55:2996
97 // Otherwise (or if that fails), create a new one.
[email protected]4566f132009-03-12 01:55:1398 if (!process_) {
[email protected]3059caa2013-06-06 03:48:4199 if (g_render_process_host_factory_) {
100 process_ = g_render_process_host_factory_->CreateRenderProcessHost(
[email protected]f681b072013-06-06 21:53:12101 browser_context, this);
[email protected]a6df5112009-01-21 23:50:15102 } else {
[email protected]4c3a23582012-08-18 08:54:34103 StoragePartitionImpl* partition =
104 static_cast<StoragePartitionImpl*>(
105 BrowserContext::GetStoragePartition(browser_context, this));
[email protected]6eb1a11e2013-10-09 00:54:37106 process_ = new RenderProcessHostImpl(browser_context,
107 partition,
[email protected]6eb1a11e2013-10-09 00:54:37108 site_.SchemeIs(kGuestScheme));
[email protected]a6df5112009-01-21 23:50:15109 }
110 }
[email protected]41fb79a52012-06-29 16:34:33111 CHECK(process_);
[email protected]40bfaf32013-11-19 01:35:43112 process_->AddObserver(this);
[email protected]41fb79a52012-06-29 16:34:33113
114 // If we are using process-per-site, we need to register this process
115 // for the current site so that we can find it again. (If no site is set
116 // at this time, we will register it in SetSite().)
117 if (use_process_per_site) {
118 RenderProcessHostImpl::RegisterProcessHostForSite(browser_context,
119 process_, site_);
120 }
initial.commit09911bf2008-07-26 23:55:29121
naskob8744d22014-08-28 17:07:43122 TRACE_EVENT2("navigation", "SiteInstanceImpl::GetProcess",
123 "site id", id_, "process id", process_->GetID());
[email protected]46488322012-10-30 03:22:20124 GetContentClient()->browser()->SiteInstanceGotProcess(this);
[email protected]6f371442011-11-09 06:45:46125
[email protected]313b80bd2011-11-23 03:49:10126 if (has_site_)
127 LockToOrigin();
initial.commit09911bf2008-07-26 23:55:29128 }
[email protected]4566f132009-03-12 01:55:13129 DCHECK(process_);
initial.commit09911bf2008-07-26 23:55:29130
[email protected]4566f132009-03-12 01:55:13131 return process_;
initial.commit09911bf2008-07-26 23:55:29132}
133
[email protected]b6583592012-01-25 19:52:33134void SiteInstanceImpl::SetSite(const GURL& url) {
naskob8744d22014-08-28 17:07:43135 TRACE_EVENT2("navigation", "SiteInstanceImpl::SetSite",
136 "site id", id_, "url", url.possibly_invalid_spec());
initial.commit09911bf2008-07-26 23:55:29137 // A SiteInstance's site should not change.
138 // TODO(creis): When following links or script navigations, we can currently
139 // render pages from other sites in this SiteInstance. This will eventually
140 // be fixed, but until then, we should still not set the site of a
141 // SiteInstance more than once.
142 DCHECK(!has_site_);
143
144 // Remember that this SiteInstance has been used to load a URL, even if the
145 // URL is invalid.
146 has_site_ = true;
[email protected]4c3a23582012-08-18 08:54:34147 BrowserContext* browser_context = browsing_instance_->browser_context();
[email protected]41fb79a52012-06-29 16:34:33148 site_ = GetSiteForURL(browser_context, url);
initial.commit09911bf2008-07-26 23:55:29149
150 // Now that we have a site, register it with the BrowsingInstance. This
151 // ensures that we won't create another SiteInstance for this site within
152 // the same BrowsingInstance, because all same-site pages within a
153 // BrowsingInstance can script each other.
154 browsing_instance_->RegisterSiteInstance(this);
[email protected]313b80bd2011-11-23 03:49:10155
[email protected]41fb79a52012-06-29 16:34:33156 if (process_) {
[email protected]313b80bd2011-11-23 03:49:10157 LockToOrigin();
[email protected]41fb79a52012-06-29 16:34:33158
159 // Ensure the process is registered for this site if necessary.
[email protected]1ae93fb12013-06-14 03:38:56160 if (RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_)) {
[email protected]41fb79a52012-06-29 16:34:33161 RenderProcessHostImpl::RegisterProcessHostForSite(
162 browser_context, process_, site_);
163 }
164 }
initial.commit09911bf2008-07-26 23:55:29165}
166
[email protected]77ab17312012-09-28 15:34:59167const GURL& SiteInstanceImpl::GetSiteURL() const {
[email protected]b6583592012-01-25 19:52:33168 return site_;
169}
170
171bool SiteInstanceImpl::HasSite() const {
172 return has_site_;
173}
174
175bool SiteInstanceImpl::HasRelatedSiteInstance(const GURL& url) {
initial.commit09911bf2008-07-26 23:55:29176 return browsing_instance_->HasSiteInstance(url);
177}
178
[email protected]b6583592012-01-25 19:52:33179SiteInstance* SiteInstanceImpl::GetRelatedSiteInstance(const GURL& url) {
initial.commit09911bf2008-07-26 23:55:29180 return browsing_instance_->GetSiteInstanceForURL(url);
181}
182
[email protected]14392a52012-05-02 20:28:44183bool SiteInstanceImpl::IsRelatedSiteInstance(const SiteInstance* instance) {
[email protected]fc72bb12013-06-02 21:13:46184 return browsing_instance_.get() == static_cast<const SiteInstanceImpl*>(
185 instance)->browsing_instance_.get();
[email protected]14392a52012-05-02 20:28:44186}
187
[email protected]d5072a82014-05-15 05:50:18188size_t SiteInstanceImpl::GetRelatedActiveContentsCount() {
189 return browsing_instance_->active_contents_count();
190}
191
[email protected]f88628d02012-11-11 17:58:59192bool SiteInstanceImpl::HasWrongProcessForURL(const GURL& url) {
[email protected]d292d8a2011-05-25 03:47:11193 // Having no process isn't a problem, since we'll assign it correctly.
[email protected]f88628d02012-11-11 17:58:59194 // Note that HasProcess() may return true if process_ is null, in
195 // process-per-site cases where there's an existing process available.
196 // We want to use such a process in the IsSuitableHost check, so we
197 // may end up assigning process_ in the GetProcess() call below.
198 if (!HasProcess())
[email protected]d292d8a2011-05-25 03:47:11199 return false;
200
[email protected]144a8102012-01-14 01:05:31201 // If the URL to navigate to can be associated with any site instance,
202 // we want to keep it in the same process.
[email protected]c02f1ba2014-02-03 06:53:53203 if (IsRendererDebugURL(url))
[email protected]144a8102012-01-14 01:05:31204 return false;
205
[email protected]88aae972011-12-16 01:14:18206 // If the site URL is an extension (e.g., for hosted apps or WebUI) but the
[email protected]d292d8a2011-05-25 03:47:11207 // process is not (or vice versa), make sure we notice and fix it.
[email protected]2a5221b2011-09-27 23:07:31208 GURL site_url = GetSiteForURL(browsing_instance_->browser_context(), url);
[email protected]88aae972011-12-16 01:14:18209 return !RenderProcessHostImpl::IsSuitableHost(
[email protected]f88628d02012-11-11 17:58:59210 GetProcess(), browsing_instance_->browser_context(), site_url);
[email protected]d292d8a2011-05-25 03:47:11211}
212
nickd30fd962015-07-27 21:51:08213bool SiteInstanceImpl::RequiresDedicatedProcess() {
214 if (!has_site_)
215 return false;
nickcc0d9142015-10-14 16:27:10216 return SiteInstanceImpl::DoesSiteRequireDedicatedProcess(GetBrowserContext(),
217 site_);
nickd30fd962015-07-27 21:51:08218}
219
[email protected]d5072a82014-05-15 05:50:18220void SiteInstanceImpl::IncrementRelatedActiveContentsCount() {
221 browsing_instance_->increment_active_contents_count();
222}
223
224void SiteInstanceImpl::DecrementRelatedActiveContentsCount() {
225 browsing_instance_->decrement_active_contents_count();
226}
227
[email protected]3059caa2013-06-06 03:48:41228void SiteInstanceImpl::set_render_process_host_factory(
229 const RenderProcessHostFactory* rph_factory) {
230 g_render_process_host_factory_ = rph_factory;
231}
232
[email protected]4c3a23582012-08-18 08:54:34233BrowserContext* SiteInstanceImpl::GetBrowserContext() const {
[email protected]72daaa92012-01-18 13:39:02234 return browsing_instance_->browser_context();
235}
236
nickcc0d9142015-10-14 16:27:10237// static
[email protected]4c3a23582012-08-18 08:54:34238SiteInstance* SiteInstance::Create(BrowserContext* browser_context) {
[email protected]b6583592012-01-25 19:52:33239 return new SiteInstanceImpl(new BrowsingInstance(browser_context));
initial.commit09911bf2008-07-26 23:55:29240}
241
nickcc0d9142015-10-14 16:27:10242// static
[email protected]4c3a23582012-08-18 08:54:34243SiteInstance* SiteInstance::CreateForURL(BrowserContext* browser_context,
244 const GURL& url) {
jinlong.zhai9f9a5352015-02-09 15:54:29245 // This will create a new SiteInstance and BrowsingInstance.
[email protected]3d7474ff2011-07-27 17:47:37246 scoped_refptr<BrowsingInstance> instance(
247 new BrowsingInstance(browser_context));
[email protected]633fbc42009-05-07 23:39:47248 return instance->GetSiteInstanceForURL(url);
[email protected]d8a96622009-05-07 19:21:16249}
250
nickcc0d9142015-10-14 16:27:10251// static
[email protected]399583b2012-12-11 09:33:42252bool SiteInstance::IsSameWebSite(BrowserContext* browser_context,
[email protected]855d7d572014-08-02 11:18:17253 const GURL& real_src_url,
254 const GURL& real_dest_url) {
255 GURL src_url = SiteInstanceImpl::GetEffectiveURL(browser_context,
256 real_src_url);
257 GURL dest_url = SiteInstanceImpl::GetEffectiveURL(browser_context,
258 real_dest_url);
[email protected]399583b2012-12-11 09:33:42259
260 // We infer web site boundaries based on the registered domain name of the
261 // top-level page and the scheme. We do not pay attention to the port if
262 // one is present, because pages served from different ports can still
263 // access each other if they change their document.domain variable.
264
265 // Some special URLs will match the site instance of any other URL. This is
266 // done before checking both of them for validity, since we want these URLs
267 // to have the same site instance as even an invalid one.
[email protected]855d7d572014-08-02 11:18:17268 if (IsRendererDebugURL(src_url) || IsRendererDebugURL(dest_url))
[email protected]399583b2012-12-11 09:33:42269 return true;
270
271 // If either URL is invalid, they aren't part of the same site.
[email protected]855d7d572014-08-02 11:18:17272 if (!src_url.is_valid() || !dest_url.is_valid())
[email protected]399583b2012-12-11 09:33:42273 return false;
274
[email protected]855d7d572014-08-02 11:18:17275 // If the destination url is just a blank page, we treat them as part of the
276 // same site.
277 GURL blank_page(url::kAboutBlankURL);
278 if (dest_url == blank_page)
279 return true;
280
[email protected]399583b2012-12-11 09:33:42281 // If the schemes differ, they aren't part of the same site.
[email protected]855d7d572014-08-02 11:18:17282 if (src_url.scheme() != dest_url.scheme())
[email protected]399583b2012-12-11 09:33:42283 return false;
284
[email protected]ed32c212013-05-14 20:49:29285 return net::registry_controlled_domains::SameDomainOrHost(
[email protected]855d7d572014-08-02 11:18:17286 src_url,
287 dest_url,
[email protected]aabe1792014-01-30 21:37:46288 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
[email protected]399583b2012-12-11 09:33:42289}
290
nickcc0d9142015-10-14 16:27:10291// static
[email protected]399583b2012-12-11 09:33:42292GURL SiteInstance::GetSiteForURL(BrowserContext* browser_context,
293 const GURL& real_url) {
[email protected]25a9e8e532012-06-22 06:16:28294 // TODO(fsamuel, creis): For some reason appID is not recognized as a host.
[email protected]6eb1a11e2013-10-09 00:54:37295 if (real_url.SchemeIs(kGuestScheme))
[email protected]25a9e8e532012-06-22 06:16:28296 return real_url;
297
[email protected]b6583592012-01-25 19:52:33298 GURL url = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url);
[email protected]3a8eecb2010-04-22 23:56:30299
initial.commit09911bf2008-07-26 23:55:29300 // If the url has a host, then determine the site.
301 if (url.has_host()) {
[email protected]6705b232008-11-26 00:16:51302 // Only keep the scheme and registered domain as given by GetOrigin. This
303 // may also include a port, which we need to drop.
creisf60c2cd2014-12-18 00:41:02304 GURL site = url.GetOrigin();
initial.commit09911bf2008-07-26 23:55:29305
[email protected]6705b232008-11-26 00:16:51306 // Remove port, if any.
307 if (site.has_port()) {
308 GURL::Replacements rep;
309 rep.ClearPort();
310 site = site.ReplaceComponents(rep);
311 }
312
initial.commit09911bf2008-07-26 23:55:29313 // If this URL has a registered domain, we only want to remember that part.
314 std::string domain =
[email protected]ed32c212013-05-14 20:49:29315 net::registry_controlled_domains::GetDomainAndRegistry(
316 url,
[email protected]aabe1792014-01-30 21:37:46317 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
initial.commit09911bf2008-07-26 23:55:29318 if (!domain.empty()) {
319 GURL::Replacements rep;
320 rep.SetHostStr(domain);
321 site = site.ReplaceComponents(rep);
322 }
creisf60c2cd2014-12-18 00:41:02323 return site;
initial.commit09911bf2008-07-26 23:55:29324 }
creisf60c2cd2014-12-18 00:41:02325
326 // If there is no host but there is a scheme, return the scheme.
327 // This is useful for cases like file URLs.
328 if (url.has_scheme())
329 return GURL(url.scheme() + ":");
330
331 // Otherwise the URL should be invalid; return an empty site.
332 DCHECK(!url.is_valid());
333 return GURL();
initial.commit09911bf2008-07-26 23:55:29334}
335
nickcc0d9142015-10-14 16:27:10336// static
[email protected]4c3a23582012-08-18 08:54:34337GURL SiteInstanceImpl::GetEffectiveURL(BrowserContext* browser_context,
338 const GURL& url) {
[email protected]46488322012-10-30 03:22:20339 return GetContentClient()->browser()->
[email protected]3d7474ff2011-07-27 17:47:37340 GetEffectiveURL(browser_context, url);
[email protected]3a8eecb2010-04-22 23:56:30341}
342
nickcc0d9142015-10-14 16:27:10343// static
344bool SiteInstanceImpl::DoesSiteRequireDedicatedProcess(
345 BrowserContext* browser_context,
346 const GURL& effective_url) {
347 // If --site-per-process is enabled, site isolation is enabled everywhere.
348 if (SiteIsolationPolicy::UseDedicatedProcessesForAllSites())
349 return true;
350
351 // Let the content embedder enable site isolation for specific URLs.
352 if (GetContentClient()->IsSupplementarySiteIsolationModeEnabled() &&
353 GetContentClient()->browser()->DoesSiteRequireDedicatedProcess(
354 browser_context, effective_url)) {
355 return true;
356 }
357
358 return false;
359}
360
[email protected]40bfaf32013-11-19 01:35:43361void SiteInstanceImpl::RenderProcessHostDestroyed(RenderProcessHost* host) {
362 DCHECK_EQ(process_, host);
363 process_->RemoveObserver(this);
364 process_ = NULL;
[email protected]4566f132009-03-12 01:55:13365}
[email protected]313b80bd2011-11-23 03:49:10366
[email protected]b6583592012-01-25 19:52:33367void SiteInstanceImpl::LockToOrigin() {
nickd30fd962015-07-27 21:51:08368 // TODO(nick): When all sites are isolated, this operation provides strong
369 // protection. If only some sites are isolated, we need additional logic to
370 // prevent the non-isolated sites from requesting resources for isolated
371 // sites. https://crbug.com/509125
nickcc0d9142015-10-14 16:27:10372 if (RequiresDedicatedProcess()) {
lazyboyf5283a42015-06-24 21:53:35373 // Guest processes cannot be locked to its site because guests always have
374 // a fixed SiteInstance. The site of GURLs a guest loads doesn't match that
375 // SiteInstance. So we skip locking the guest process to the site.
376 // TODO(ncarter): Remove this exclusion once we can make origin lock per
377 // RenderFrame routing id.
378 if (site_.SchemeIs(content::kGuestScheme))
379 return;
380
lfg24d083792015-07-17 20:45:35381 // TODO(creis, nick) https://crbug.com/510588 Chrome UI pages use the same
382 // site (chrome://chrome), so they can't be locked because the site being
383 // loaded doesn't match the SiteInstance.
384 if (site_.SchemeIs(content::kChromeUIScheme))
385 return;
386
creise5d6d1732015-08-25 19:47:06387 // TODO(creis, nick): Until we can handle sites with effective URLs at the
388 // call sites of ChildProcessSecurityPolicy::CanAccessDataForOrigin, we
389 // must give the embedder a chance to exempt some sites to avoid process
390 // kills.
nick7d0984c2015-08-29 00:13:46391 if (!GetContentClient()->browser()->ShouldLockToOrigin(
392 browsing_instance_->browser_context(), site_))
creise5d6d1732015-08-25 19:47:06393 return;
394
[email protected]b9535422012-02-09 01:47:59395 ChildProcessSecurityPolicyImpl* policy =
396 ChildProcessSecurityPolicyImpl::GetInstance();
[email protected]313b80bd2011-11-23 03:49:10397 policy->LockToOrigin(process_->GetID(), site_);
398 }
399}
[email protected]46488322012-10-30 03:22:20400
401} // namespace content