blob: 7a5ed8e4c3f0d9e64296a6553d8d9aecc5bd345e [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
alexmos3b9ad102017-05-26 23:41:087#include "base/macros.h"
avicb129c02017-05-03 06:49:298#include "base/memory/ptr_util.h"
[email protected]39365212011-02-24 01:01:009#include "content/browser/browsing_instance.h"
[email protected]b9535422012-02-09 01:47:5910#include "content/browser/child_process_security_policy_impl.h"
[email protected]c02f1ba2014-02-03 06:53:5311#include "content/browser/frame_host/debug_urls.h"
nick9f34e2892016-01-12 21:01:0712#include "content/browser/frame_host/frame_tree_node.h"
[email protected]f3b1a082011-11-18 00:34:3013#include "content/browser/renderer_host/render_process_host_impl.h"
[email protected]4c3a23582012-08-18 08:54:3414#include "content/browser/storage_partition_impl.h"
nickd30fd962015-07-27 21:51:0815#include "content/common/site_isolation_policy.h"
[email protected]87f3c082011-10-19 18:07:4416#include "content/public/browser/content_browser_client.h"
[email protected]f3b1a082011-11-18 00:34:3017#include "content/public/browser/render_process_host_factory.h"
[email protected]41fb79a52012-06-29 16:34:3318#include "content/public/browser/web_ui_controller_factory.h"
[email protected]a1d29162011-10-14 17:14:0319#include "content/public/common/url_constants.h"
[email protected]be28b5f42012-07-20 11:31:2520#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
initial.commit09911bf2008-07-26 23:55:2921
[email protected]46488322012-10-30 03:22:2022namespace content {
[email protected]b6583592012-01-25 19:52:3323
avib7348942015-12-25 20:57:1024int32_t SiteInstanceImpl::next_site_instance_id_ = 1;
[email protected]992db4c2011-05-12 15:37:1525
[email protected]b6583592012-01-25 19:52:3326SiteInstanceImpl::SiteInstanceImpl(BrowsingInstance* browsing_instance)
[email protected]992db4c2011-05-12 15:37:1527 : id_(next_site_instance_id_++),
creiscce56cd2014-09-29 22:45:2228 active_frame_count_(0),
[email protected]992db4c2011-05-12 15:37:1529 browsing_instance_(browsing_instance),
nickd5bbd0b2016-03-31 19:52:4430 process_(nullptr),
31 has_site_(false),
Tsuyoshi Horoeb576e62017-06-28 06:00:4132 process_reuse_policy_(ProcessReusePolicy::DEFAULT),
33 is_for_service_worker_(false) {
[email protected]4566f132009-03-12 01:55:1334 DCHECK(browsing_instance);
[email protected]4566f132009-03-12 01:55:1335}
36
[email protected]b6583592012-01-25 19:52:3337SiteInstanceImpl::~SiteInstanceImpl() {
[email protected]46488322012-10-30 03:22:2038 GetContentClient()->browser()->SiteInstanceDeleting(this);
[email protected]056ad2a2011-07-12 02:13:5539
[email protected]40bfaf32013-11-19 01:35:4340 if (process_)
41 process_->RemoveObserver(this);
42
initial.commit09911bf2008-07-26 23:55:2943 // Now that no one is referencing us, we can safely remove ourselves from
44 // the BrowsingInstance. Any future visits to a page from this site
45 // (within the same BrowsingInstance) can safely create a new SiteInstance.
46 if (has_site_)
dchengbccd6b82016-03-30 16:24:1947 browsing_instance_->UnregisterSiteInstance(this);
48}
49
50scoped_refptr<SiteInstanceImpl> SiteInstanceImpl::Create(
51 BrowserContext* browser_context) {
52 return make_scoped_refptr(
53 new SiteInstanceImpl(new BrowsingInstance(browser_context)));
54}
55
56scoped_refptr<SiteInstanceImpl> SiteInstanceImpl::CreateForURL(
57 BrowserContext* browser_context,
58 const GURL& url) {
59 // This will create a new SiteInstance and BrowsingInstance.
60 scoped_refptr<BrowsingInstance> instance(
61 new BrowsingInstance(browser_context));
62 return instance->GetSiteInstanceForURL(url);
initial.commit09911bf2008-07-26 23:55:2963}
64
avib7348942015-12-25 20:57:1065int32_t SiteInstanceImpl::GetId() {
[email protected]b6583592012-01-25 19:52:3366 return id_;
67}
68
69bool SiteInstanceImpl::HasProcess() const {
[email protected]41fb79a52012-06-29 16:34:3370 if (process_ != NULL)
71 return true;
72
73 // If we would use process-per-site for this site, also check if there is an
74 // existing process that we would use if GetProcess() were called.
[email protected]46488322012-10-30 03:22:2075 BrowserContext* browser_context =
[email protected]41fb79a52012-06-29 16:34:3376 browsing_instance_->browser_context();
77 if (has_site_ &&
[email protected]1ae93fb12013-06-14 03:38:5678 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_) &&
[email protected]41fb79a52012-06-29 16:34:3379 RenderProcessHostImpl::GetProcessHostForSite(browser_context, site_)) {
80 return true;
81 }
82
83 return false;
[email protected]5ab79b02010-04-26 16:47:1184}
85
[email protected]41fb79a52012-06-29 16:34:3386RenderProcessHost* SiteInstanceImpl::GetProcess() {
[email protected]08c8ec7e2010-07-11 17:14:4887 // TODO(erikkay) It would be nice to ensure that the renderer type had been
88 // properly set before we get here. The default tab creation case winds up
89 // with no site set at this point, so it will default to TYPE_NORMAL. This
90 // may not be correct, so we'll wind up potentially creating a process that
91 // we then throw away, or worse sharing a process with the wrong process type.
92 // See crbug.com/43448.
93
initial.commit09911bf2008-07-26 23:55:2994 // Create a new process if ours went away or was reused.
[email protected]4566f132009-03-12 01:55:1395 if (!process_) {
[email protected]4c3a23582012-08-18 08:54:3496 BrowserContext* browser_context = browsing_instance_->browser_context();
[email protected]41fb79a52012-06-29 16:34:3397
clamy63960c32017-05-10 22:57:0798 // Check if the ProcessReusePolicy should be updated.
99 bool should_use_process_per_site =
creisa9f04ba2017-05-19 22:27:42100 has_site_ &&
101 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_);
clamy63960c32017-05-10 22:57:07102 if (should_use_process_per_site) {
103 process_reuse_policy_ = ProcessReusePolicy::PROCESS_PER_SITE;
104 } else if (process_reuse_policy_ == ProcessReusePolicy::PROCESS_PER_SITE) {
105 process_reuse_policy_ = ProcessReusePolicy::DEFAULT;
[email protected]41fb79a52012-06-29 16:34:33106 }
107
clamy63960c32017-05-10 22:57:07108 process_ = RenderProcessHostImpl::GetProcessHostForSiteInstance(
109 browser_context, this);
avi6d686db2017-01-06 02:28:57110
[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().)
clamy63960c32017-05-10 22:57:07117 if (process_reuse_policy_ == ProcessReusePolicy::PROCESS_PER_SITE &&
118 has_site_) {
[email protected]41fb79a52012-06-29 16:34:33119 RenderProcessHostImpl::RegisterProcessHostForSite(browser_context,
120 process_, site_);
121 }
initial.commit09911bf2008-07-26 23:55:29122
naskob8744d22014-08-28 17:07:43123 TRACE_EVENT2("navigation", "SiteInstanceImpl::GetProcess",
124 "site id", id_, "process id", process_->GetID());
[email protected]46488322012-10-30 03:22:20125 GetContentClient()->browser()->SiteInstanceGotProcess(this);
[email protected]6f371442011-11-09 06:45:46126
[email protected]313b80bd2011-11-23 03:49:10127 if (has_site_)
alexmos13fe1962017-06-28 04:25:12128 LockToOriginIfNeeded();
initial.commit09911bf2008-07-26 23:55:29129 }
[email protected]4566f132009-03-12 01:55:13130 DCHECK(process_);
initial.commit09911bf2008-07-26 23:55:29131
[email protected]4566f132009-03-12 01:55:13132 return process_;
initial.commit09911bf2008-07-26 23:55:29133}
134
[email protected]b6583592012-01-25 19:52:33135void SiteInstanceImpl::SetSite(const GURL& url) {
naskob8744d22014-08-28 17:07:43136 TRACE_EVENT2("navigation", "SiteInstanceImpl::SetSite",
137 "site id", id_, "url", url.possibly_invalid_spec());
initial.commit09911bf2008-07-26 23:55:29138 // 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.
143 DCHECK(!has_site_);
144
145 // Remember that this SiteInstance has been used to load a URL, even if the
146 // URL is invalid.
147 has_site_ = true;
[email protected]4c3a23582012-08-18 08:54:34148 BrowserContext* browser_context = browsing_instance_->browser_context();
[email protected]41fb79a52012-06-29 16:34:33149 site_ = GetSiteForURL(browser_context, url);
initial.commit09911bf2008-07-26 23:55:29150
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);
[email protected]313b80bd2011-11-23 03:49:10156
clamy63960c32017-05-10 22:57:07157 // Update the process reuse policy based on the site.
158 bool should_use_process_per_site =
159 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_);
160 if (should_use_process_per_site) {
161 process_reuse_policy_ = ProcessReusePolicy::PROCESS_PER_SITE;
162 }
163
[email protected]41fb79a52012-06-29 16:34:33164 if (process_) {
alexmos13fe1962017-06-28 04:25:12165 LockToOriginIfNeeded();
[email protected]41fb79a52012-06-29 16:34:33166
167 // Ensure the process is registered for this site if necessary.
clamy63960c32017-05-10 22:57:07168 if (should_use_process_per_site) {
[email protected]41fb79a52012-06-29 16:34:33169 RenderProcessHostImpl::RegisterProcessHostForSite(
170 browser_context, process_, site_);
171 }
172 }
initial.commit09911bf2008-07-26 23:55:29173}
174
[email protected]77ab17312012-09-28 15:34:59175const GURL& SiteInstanceImpl::GetSiteURL() const {
[email protected]b6583592012-01-25 19:52:33176 return site_;
177}
178
179bool SiteInstanceImpl::HasSite() const {
180 return has_site_;
181}
182
183bool SiteInstanceImpl::HasRelatedSiteInstance(const GURL& url) {
initial.commit09911bf2008-07-26 23:55:29184 return browsing_instance_->HasSiteInstance(url);
185}
186
dchengbccd6b82016-03-30 16:24:19187scoped_refptr<SiteInstance> SiteInstanceImpl::GetRelatedSiteInstance(
188 const GURL& url) {
initial.commit09911bf2008-07-26 23:55:29189 return browsing_instance_->GetSiteInstanceForURL(url);
190}
191
[email protected]14392a52012-05-02 20:28:44192bool SiteInstanceImpl::IsRelatedSiteInstance(const SiteInstance* instance) {
[email protected]fc72bb12013-06-02 21:13:46193 return browsing_instance_.get() == static_cast<const SiteInstanceImpl*>(
194 instance)->browsing_instance_.get();
[email protected]14392a52012-05-02 20:28:44195}
196
[email protected]d5072a82014-05-15 05:50:18197size_t SiteInstanceImpl::GetRelatedActiveContentsCount() {
198 return browsing_instance_->active_contents_count();
199}
200
[email protected]f88628d02012-11-11 17:58:59201bool SiteInstanceImpl::HasWrongProcessForURL(const GURL& url) {
[email protected]d292d8a2011-05-25 03:47:11202 // Having no process isn't a problem, since we'll assign it correctly.
[email protected]f88628d02012-11-11 17:58:59203 // Note that HasProcess() may return true if process_ is null, in
204 // process-per-site cases where there's an existing process available.
205 // We want to use such a process in the IsSuitableHost check, so we
206 // may end up assigning process_ in the GetProcess() call below.
207 if (!HasProcess())
[email protected]d292d8a2011-05-25 03:47:11208 return false;
209
[email protected]144a8102012-01-14 01:05:31210 // If the URL to navigate to can be associated with any site instance,
211 // we want to keep it in the same process.
[email protected]c02f1ba2014-02-03 06:53:53212 if (IsRendererDebugURL(url))
[email protected]144a8102012-01-14 01:05:31213 return false;
214
alexmos13fe1962017-06-28 04:25:12215 // Any process can host an about:blank URL. This check avoids a process
216 // transfer for browser-initiated navigations to about:blank in a dedicated
217 // process; without it, IsSuitableHost would consider this process unsuitable
218 // for about:blank when it compares origin locks. Renderer-initiated
219 // navigations will handle about:blank navigations elsewhere and leave them
220 // in the source SiteInstance, along with about:srcdoc and data:.
221 if (url == url::kAboutBlankURL)
222 return false;
223
[email protected]88aae972011-12-16 01:14:18224 // If the site URL is an extension (e.g., for hosted apps or WebUI) but the
[email protected]d292d8a2011-05-25 03:47:11225 // process is not (or vice versa), make sure we notice and fix it.
[email protected]2a5221b2011-09-27 23:07:31226 GURL site_url = GetSiteForURL(browsing_instance_->browser_context(), url);
[email protected]88aae972011-12-16 01:14:18227 return !RenderProcessHostImpl::IsSuitableHost(
[email protected]f88628d02012-11-11 17:58:59228 GetProcess(), browsing_instance_->browser_context(), site_url);
[email protected]d292d8a2011-05-25 03:47:11229}
230
nickd5bbd0b2016-03-31 19:52:44231scoped_refptr<SiteInstanceImpl>
232SiteInstanceImpl::GetDefaultSubframeSiteInstance() {
233 return browsing_instance_->GetDefaultSubframeSiteInstance();
234}
235
nickd30fd962015-07-27 21:51:08236bool SiteInstanceImpl::RequiresDedicatedProcess() {
237 if (!has_site_)
238 return false;
nickd5bbd0b2016-03-31 19:52:44239
240 return DoesSiteRequireDedicatedProcess(GetBrowserContext(), site_);
nickd30fd962015-07-27 21:51:08241}
242
avi85cacb72016-10-26 22:39:33243bool SiteInstanceImpl::IsDefaultSubframeSiteInstance() const {
clamy63960c32017-05-10 22:57:07244 return process_reuse_policy_ ==
245 ProcessReusePolicy::USE_DEFAULT_SUBFRAME_PROCESS;
avi85cacb72016-10-26 22:39:33246}
247
nick9f34e2892016-01-12 21:01:07248void SiteInstanceImpl::IncrementActiveFrameCount() {
249 active_frame_count_++;
250}
251
252void SiteInstanceImpl::DecrementActiveFrameCount() {
ericwilligers254597b2016-10-17 10:32:31253 if (--active_frame_count_ == 0) {
254 for (auto& observer : observers_)
255 observer.ActiveFrameCountIsZero(this);
256 }
nick9f34e2892016-01-12 21:01:07257}
258
[email protected]d5072a82014-05-15 05:50:18259void SiteInstanceImpl::IncrementRelatedActiveContentsCount() {
260 browsing_instance_->increment_active_contents_count();
261}
262
263void SiteInstanceImpl::DecrementRelatedActiveContentsCount() {
264 browsing_instance_->decrement_active_contents_count();
265}
266
nick9f34e2892016-01-12 21:01:07267void SiteInstanceImpl::AddObserver(Observer* observer) {
268 observers_.AddObserver(observer);
269}
270
271void SiteInstanceImpl::RemoveObserver(Observer* observer) {
272 observers_.RemoveObserver(observer);
273}
274
[email protected]4c3a23582012-08-18 08:54:34275BrowserContext* SiteInstanceImpl::GetBrowserContext() const {
[email protected]72daaa92012-01-18 13:39:02276 return browsing_instance_->browser_context();
277}
278
nickcc0d9142015-10-14 16:27:10279// static
dchengbccd6b82016-03-30 16:24:19280scoped_refptr<SiteInstance> SiteInstance::Create(
281 BrowserContext* browser_context) {
282 return SiteInstanceImpl::Create(browser_context);
initial.commit09911bf2008-07-26 23:55:29283}
284
nickcc0d9142015-10-14 16:27:10285// static
dchengbccd6b82016-03-30 16:24:19286scoped_refptr<SiteInstance> SiteInstance::CreateForURL(
287 BrowserContext* browser_context,
288 const GURL& url) {
289 return SiteInstanceImpl::CreateForURL(browser_context, url);
[email protected]d8a96622009-05-07 19:21:16290}
291
nickcc0d9142015-10-14 16:27:10292// static
[email protected]399583b2012-12-11 09:33:42293bool SiteInstance::IsSameWebSite(BrowserContext* browser_context,
[email protected]855d7d572014-08-02 11:18:17294 const GURL& real_src_url,
295 const GURL& real_dest_url) {
296 GURL src_url = SiteInstanceImpl::GetEffectiveURL(browser_context,
297 real_src_url);
298 GURL dest_url = SiteInstanceImpl::GetEffectiveURL(browser_context,
299 real_dest_url);
[email protected]399583b2012-12-11 09:33:42300
301 // We infer web site boundaries based on the registered domain name of the
302 // top-level page and the scheme. We do not pay attention to the port if
303 // one is present, because pages served from different ports can still
304 // access each other if they change their document.domain variable.
305
306 // Some special URLs will match the site instance of any other URL. This is
307 // done before checking both of them for validity, since we want these URLs
308 // to have the same site instance as even an invalid one.
[email protected]855d7d572014-08-02 11:18:17309 if (IsRendererDebugURL(src_url) || IsRendererDebugURL(dest_url))
[email protected]399583b2012-12-11 09:33:42310 return true;
311
312 // If either URL is invalid, they aren't part of the same site.
[email protected]855d7d572014-08-02 11:18:17313 if (!src_url.is_valid() || !dest_url.is_valid())
[email protected]399583b2012-12-11 09:33:42314 return false;
315
[email protected]855d7d572014-08-02 11:18:17316 // If the destination url is just a blank page, we treat them as part of the
317 // same site.
318 GURL blank_page(url::kAboutBlankURL);
319 if (dest_url == blank_page)
320 return true;
321
alexmos3b9ad102017-05-26 23:41:08322 url::Origin src_origin(src_url);
323 url::Origin dest_origin(dest_url);
alexmos3b9ad102017-05-26 23:41:08324
[email protected]399583b2012-12-11 09:33:42325 // If the schemes differ, they aren't part of the same site.
alexmoscbf995782017-06-01 03:13:13326 if (src_origin.scheme() != dest_origin.scheme())
[email protected]399583b2012-12-11 09:33:42327 return false;
328
alexmos4bc26322017-07-01 00:57:14329 if (!net::registry_controlled_domains::SameDomainOrHost(
330 src_origin, dest_origin,
331 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) {
332 return false;
333 }
334
335 // If the sites are the same, check isolated origins. If either URL matches
336 // an isolated origin, compare origins rather than sites.
337 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
338 url::Origin src_isolated_origin;
339 url::Origin dest_isolated_origin;
340 bool src_origin_is_isolated =
341 policy->GetMatchingIsolatedOrigin(src_origin, &src_isolated_origin);
342 bool dest_origin_is_isolated =
343 policy->GetMatchingIsolatedOrigin(dest_origin, &dest_isolated_origin);
344 if (src_origin_is_isolated || dest_origin_is_isolated) {
345 // Compare most specific matching origins to ensure that a subdomain of an
346 // isolated origin (e.g., https://subdomain.isolated.foo.com) also matches
347 // the isolated origin's site URL (e.g., https://isolated.foo.com).
348 return src_isolated_origin == dest_isolated_origin;
349 }
350
351 return true;
[email protected]399583b2012-12-11 09:33:42352}
353
nickcc0d9142015-10-14 16:27:10354// static
[email protected]399583b2012-12-11 09:33:42355GURL SiteInstance::GetSiteForURL(BrowserContext* browser_context,
356 const GURL& real_url) {
[email protected]25a9e8e532012-06-22 06:16:28357 // TODO(fsamuel, creis): For some reason appID is not recognized as a host.
[email protected]6eb1a11e2013-10-09 00:54:37358 if (real_url.SchemeIs(kGuestScheme))
[email protected]25a9e8e532012-06-22 06:16:28359 return real_url;
360
[email protected]b6583592012-01-25 19:52:33361 GURL url = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url);
nick1dd47922016-04-29 16:44:48362 url::Origin origin(url);
[email protected]3a8eecb2010-04-22 23:56:30363
alexmos4bc26322017-07-01 00:57:14364 // Isolated origins should use the full origin as their site URL. A subdomain
365 // of an isolated origin should also use that isolated origin's site URL.
alexmos3b9ad102017-05-26 23:41:08366 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
alexmos4bc26322017-07-01 00:57:14367 url::Origin isolated_origin;
368 if (policy->GetMatchingIsolatedOrigin(url::Origin(real_url),
369 &isolated_origin)) {
370 return isolated_origin.GetURL();
371 }
alexmos3b9ad102017-05-26 23:41:08372
initial.commit09911bf2008-07-26 23:55:29373 // If the url has a host, then determine the site.
nick1dd47922016-04-29 16:44:48374 if (!origin.host().empty()) {
375 // Only keep the scheme and registered domain of |origin|.
376 std::string domain = net::registry_controlled_domains::GetDomainAndRegistry(
377 origin.host(),
378 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
379 std::string site = origin.scheme();
380 site += url::kStandardSchemeSeparator;
381 site += domain.empty() ? origin.host() : domain;
382 return GURL(site);
initial.commit09911bf2008-07-26 23:55:29383 }
creisf60c2cd2014-12-18 00:41:02384
385 // If there is no host but there is a scheme, return the scheme.
386 // This is useful for cases like file URLs.
387 if (url.has_scheme())
388 return GURL(url.scheme() + ":");
389
390 // Otherwise the URL should be invalid; return an empty site.
391 DCHECK(!url.is_valid());
392 return GURL();
initial.commit09911bf2008-07-26 23:55:29393}
394
nickcc0d9142015-10-14 16:27:10395// static
[email protected]4c3a23582012-08-18 08:54:34396GURL SiteInstanceImpl::GetEffectiveURL(BrowserContext* browser_context,
397 const GURL& url) {
alexmos3b9ad102017-05-26 23:41:08398 // Don't resolve URLs corresponding to isolated origins, as isolated origins
399 // take precedence over hosted apps.
400 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
401 if (policy->IsIsolatedOrigin(url::Origin(url)))
402 return url;
403
[email protected]46488322012-10-30 03:22:20404 return GetContentClient()->browser()->
[email protected]3d7474ff2011-07-27 17:47:37405 GetEffectiveURL(browser_context, url);
[email protected]3a8eecb2010-04-22 23:56:30406}
407
nickcc0d9142015-10-14 16:27:10408// static
409bool SiteInstanceImpl::DoesSiteRequireDedicatedProcess(
410 BrowserContext* browser_context,
nickdb193a12016-09-09 23:09:23411 const GURL& url) {
nickcc0d9142015-10-14 16:27:10412 // If --site-per-process is enabled, site isolation is enabled everywhere.
413 if (SiteIsolationPolicy::UseDedicatedProcessesForAllSites())
414 return true;
415
alexmos3b9ad102017-05-26 23:41:08416 // Always require a dedicated process for isolated origins.
417 GURL site_url = GetSiteForURL(browser_context, url);
418 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
419 if (policy->IsIsolatedOrigin(url::Origin(site_url)))
420 return true;
421
nickdb193a12016-09-09 23:09:23422 // Let the content embedder enable site isolation for specific URLs. Use the
423 // canonical site url for this check, so that schemes with nested origins
424 // (blob and filesystem) work properly.
nickcc0d9142015-10-14 16:27:10425 if (GetContentClient()->IsSupplementarySiteIsolationModeEnabled() &&
426 GetContentClient()->browser()->DoesSiteRequireDedicatedProcess(
nickdb193a12016-09-09 23:09:23427 browser_context, site_url)) {
nickcc0d9142015-10-14 16:27:10428 return true;
429 }
430
431 return false;
432}
433
alexmos13fe1962017-06-28 04:25:12434// static
435bool SiteInstanceImpl::ShouldLockToOrigin(BrowserContext* browser_context,
436 GURL site_url) {
437 if (!DoesSiteRequireDedicatedProcess(browser_context, site_url))
438 return false;
439
440 // Guest processes cannot be locked to their site because guests always have
441 // a fixed SiteInstance. The site of GURLs a guest loads doesn't match that
442 // SiteInstance. So we skip locking the guest process to the site.
443 // TODO(ncarter): Remove this exclusion once we can make origin lock per
444 // RenderFrame routing id.
445 if (site_url.SchemeIs(content::kGuestScheme))
446 return false;
447
448 // TODO(creis, nick) https://crbug.com/510588 Chrome UI pages use the same
449 // site (chrome://chrome), so they can't be locked because the site being
450 // loaded doesn't match the SiteInstance.
451 if (site_url.SchemeIs(content::kChromeUIScheme))
452 return false;
453
454 // TODO(creis, nick): Until we can handle sites with effective URLs at the
455 // call sites of ChildProcessSecurityPolicy::CanAccessDataForOrigin, we
456 // must give the embedder a chance to exempt some sites to avoid process
457 // kills.
458 if (!GetContentClient()->browser()->ShouldLockToOrigin(browser_context,
459 site_url)) {
460 return false;
461 }
462
463 return true;
464}
465
[email protected]40bfaf32013-11-19 01:35:43466void SiteInstanceImpl::RenderProcessHostDestroyed(RenderProcessHost* host) {
467 DCHECK_EQ(process_, host);
468 process_->RemoveObserver(this);
nickd5bbd0b2016-03-31 19:52:44469 process_ = nullptr;
[email protected]4566f132009-03-12 01:55:13470}
[email protected]313b80bd2011-11-23 03:49:10471
nick9f34e2892016-01-12 21:01:07472void SiteInstanceImpl::RenderProcessWillExit(RenderProcessHost* host) {
473 // TODO(nick): http://crbug.com/575400 - RenderProcessWillExit might not serve
474 // any purpose here.
ericwilligers254597b2016-10-17 10:32:31475 for (auto& observer : observers_)
476 observer.RenderProcessGone(this);
nick9f34e2892016-01-12 21:01:07477}
478
479void SiteInstanceImpl::RenderProcessExited(RenderProcessHost* host,
480 base::TerminationStatus status,
481 int exit_code) {
ericwilligers254597b2016-10-17 10:32:31482 for (auto& observer : observers_)
483 observer.RenderProcessGone(this);
nick9f34e2892016-01-12 21:01:07484}
485
alexmos13fe1962017-06-28 04:25:12486void SiteInstanceImpl::LockToOriginIfNeeded() {
487 DCHECK(HasSite());
488
489 // From now on, this process should be considered "tainted" for future
490 // process reuse decisions:
491 // (1) If |site_| required a dedicated process, this SiteInstance's process
492 // can only host URLs for the same site.
493 // (2) Even if |site_| does not require a dedicated process, this
494 // SiteInstance's process still cannot be reused to host other sites
495 // requiring dedicated sites in the future.
496 // We can get here either when we commit a URL into a SiteInstance that does
497 // not yet have a site, or when we create a process for a SiteInstance with a
498 // preassigned site.
499 process_->SetIsUsed();
500
nickd30fd962015-07-27 21:51:08501 // TODO(nick): When all sites are isolated, this operation provides strong
502 // protection. If only some sites are isolated, we need additional logic to
503 // prevent the non-isolated sites from requesting resources for isolated
504 // sites. https://crbug.com/509125
alexmos13fe1962017-06-28 04:25:12505 if (ShouldLockToOrigin(GetBrowserContext(), site_)) {
[email protected]b9535422012-02-09 01:47:59506 ChildProcessSecurityPolicyImpl* policy =
507 ChildProcessSecurityPolicyImpl::GetInstance();
[email protected]313b80bd2011-11-23 03:49:10508 policy->LockToOrigin(process_->GetID(), site_);
509 }
510}
[email protected]46488322012-10-30 03:22:20511
512} // namespace content