blob: 1612c78da0ff0179540085bd1a5834fc2619afb0 [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
Lukasz Anforowicz48097c42017-12-15 00:23:387#include <string>
8
creis0b49fa12017-07-10 16:31:119#include "base/command_line.h"
Alex Moshchuk4479b97c2017-10-17 23:20:3210#include "base/debug/crash_logging.h"
alexmos3b9ad102017-05-26 23:41:0811#include "base/macros.h"
[email protected]39365212011-02-24 01:01:0012#include "content/browser/browsing_instance.h"
[email protected]b9535422012-02-09 01:47:5913#include "content/browser/child_process_security_policy_impl.h"
[email protected]c02f1ba2014-02-03 06:53:5314#include "content/browser/frame_host/debug_urls.h"
nick9f34e2892016-01-12 21:01:0715#include "content/browser/frame_host/frame_tree_node.h"
Alex Moshchuk07e1bb42019-03-08 04:44:0816#include "content/browser/isolated_origin_util.h"
Alex Moshchuk8e5c1952019-01-15 03:39:5017#include "content/browser/isolation_context.h"
[email protected]f3b1a082011-11-18 00:34:3018#include "content/browser/renderer_host/render_process_host_impl.h"
[email protected]4c3a23582012-08-18 08:54:3419#include "content/browser/storage_partition_impl.h"
Aaron Colwellea6921f2019-01-29 16:50:3920#include "content/public/browser/browser_or_resource_context.h"
[email protected]87f3c082011-10-19 18:07:4421#include "content/public/browser/content_browser_client.h"
[email protected]f3b1a082011-11-18 00:34:3022#include "content/public/browser/render_process_host_factory.h"
Nick Carterbf6264a52018-04-06 02:39:3323#include "content/public/browser/site_isolation_policy.h"
[email protected]41fb79a52012-06-29 16:34:3324#include "content/public/browser/web_ui_controller_factory.h"
Nate Chapin71da03c2019-02-05 01:21:4125#include "content/public/common/content_features.h"
creis0b49fa12017-07-10 16:31:1126#include "content/public/common/content_switches.h"
[email protected]a1d29162011-10-14 17:14:0327#include "content/public/common/url_constants.h"
clamy7fced7b2017-11-16 19:52:4328#include "content/public/common/url_utils.h"
[email protected]be28b5f42012-07-20 11:31:2529#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
initial.commit09911bf2008-07-26 23:55:2930
[email protected]46488322012-10-30 03:22:2031namespace content {
[email protected]b6583592012-01-25 19:52:3332
avib7348942015-12-25 20:57:1033int32_t SiteInstanceImpl::next_site_instance_id_ = 1;
[email protected]992db4c2011-05-12 15:37:1534
Alex Moshchuk1656c672018-04-28 01:48:3435using CheckOriginLockResult =
36 ChildProcessSecurityPolicyImpl::CheckOriginLockResult;
37
[email protected]b6583592012-01-25 19:52:3338SiteInstanceImpl::SiteInstanceImpl(BrowsingInstance* browsing_instance)
[email protected]992db4c2011-05-12 15:37:1539 : id_(next_site_instance_id_++),
creiscce56cd2014-09-29 22:45:2240 active_frame_count_(0),
[email protected]992db4c2011-05-12 15:37:1541 browsing_instance_(browsing_instance),
nickd5bbd0b2016-03-31 19:52:4442 process_(nullptr),
Lukasz Anforowicz3caa8372018-06-05 17:22:0743 can_associate_with_spare_process_(true),
nickd5bbd0b2016-03-31 19:52:4444 has_site_(false),
Tsuyoshi Horoeb576e62017-06-28 06:00:4145 process_reuse_policy_(ProcessReusePolicy::DEFAULT),
46 is_for_service_worker_(false) {
[email protected]4566f132009-03-12 01:55:1347 DCHECK(browsing_instance);
[email protected]4566f132009-03-12 01:55:1348}
49
[email protected]b6583592012-01-25 19:52:3350SiteInstanceImpl::~SiteInstanceImpl() {
[email protected]46488322012-10-30 03:22:2051 GetContentClient()->browser()->SiteInstanceDeleting(this);
[email protected]056ad2a2011-07-12 02:13:5552
[email protected]40bfaf32013-11-19 01:35:4353 if (process_)
54 process_->RemoveObserver(this);
55
initial.commit09911bf2008-07-26 23:55:2956 // Now that no one is referencing us, we can safely remove ourselves from
57 // the BrowsingInstance. Any future visits to a page from this site
58 // (within the same BrowsingInstance) can safely create a new SiteInstance.
59 if (has_site_)
dchengbccd6b82016-03-30 16:24:1960 browsing_instance_->UnregisterSiteInstance(this);
61}
62
Nasko Oskovdeb6c7ea2017-11-30 18:18:1163// static
dchengbccd6b82016-03-30 16:24:1964scoped_refptr<SiteInstanceImpl> SiteInstanceImpl::Create(
65 BrowserContext* browser_context) {
Lukasz Anforowicz4726a172018-10-15 21:25:1066 DCHECK(browser_context);
kylecharda69d882017-10-04 05:49:5267 return base::WrapRefCounted(
dchengbccd6b82016-03-30 16:24:1968 new SiteInstanceImpl(new BrowsingInstance(browser_context)));
69}
70
Nasko Oskovdeb6c7ea2017-11-30 18:18:1171// static
dchengbccd6b82016-03-30 16:24:1972scoped_refptr<SiteInstanceImpl> SiteInstanceImpl::CreateForURL(
73 BrowserContext* browser_context,
74 const GURL& url) {
Lukasz Anforowicz4726a172018-10-15 21:25:1075 DCHECK(browser_context);
dchengbccd6b82016-03-30 16:24:1976 // This will create a new SiteInstance and BrowsingInstance.
77 scoped_refptr<BrowsingInstance> instance(
78 new BrowsingInstance(browser_context));
Aaron Colwellddeccbdb2019-03-08 01:11:0379 return instance->GetSiteInstanceForURL(url,
80 /* allow_default_instance */ false);
initial.commit09911bf2008-07-26 23:55:2981}
82
Nasko Oskovdeb6c7ea2017-11-30 18:18:1183// static
84bool SiteInstanceImpl::ShouldAssignSiteForURL(const GURL& url) {
85 // about:blank should not "use up" a new SiteInstance. The SiteInstance can
86 // still be used for a normal web site.
87 if (url == url::kAboutBlankURL)
88 return false;
89
90 // The embedder will then have the opportunity to determine if the URL
91 // should "use up" the SiteInstance.
92 return GetContentClient()->browser()->ShouldAssignSiteForURL(url);
93}
94
Nicolas Pena7c7847f2018-05-30 01:36:0595// static
96bool SiteInstanceImpl::IsOriginLockASite(const GURL& lock_url) {
97 return lock_url.has_scheme() && lock_url.has_host();
98}
99
avib7348942015-12-25 20:57:10100int32_t SiteInstanceImpl::GetId() {
[email protected]b6583592012-01-25 19:52:33101 return id_;
102}
103
Alex Moshchuk8e5c1952019-01-15 03:39:50104const IsolationContext& SiteInstanceImpl::GetIsolationContext() {
105 return browsing_instance_->isolation_context();
106}
107
Nate Chapin71da03c2019-02-05 01:21:41108RenderProcessHost* SiteInstanceImpl::GetDefaultProcessIfUsable() {
109 if (!base::FeatureList::IsEnabled(
110 features::kProcessSharingWithStrictSiteInstances)) {
111 return nullptr;
112 }
113 if (RequiresDedicatedProcess())
114 return nullptr;
115 return browsing_instance_->default_process();
116}
117
Aaron Colwellddeccbdb2019-03-08 01:11:03118bool SiteInstanceImpl::IsDefaultSiteInstance() {
119 return browsing_instance_->IsDefaultSiteInstance(this);
120}
121
Nate Chapin71da03c2019-02-05 01:21:41122void SiteInstanceImpl::MaybeSetBrowsingInstanceDefaultProcess() {
123 if (!base::FeatureList::IsEnabled(
124 features::kProcessSharingWithStrictSiteInstances)) {
125 return;
126 }
127 // Wait until this SiteInstance both has a site and a process
128 // assigned, so that we can be sure that RequiresDedicatedProcess()
129 // is accurate and we actually have a process to set.
130 if (!process_ || !has_site_ || RequiresDedicatedProcess())
131 return;
132 if (browsing_instance_->default_process()) {
133 DCHECK_EQ(process_, browsing_instance_->default_process());
134 return;
135 }
136 browsing_instance_->SetDefaultProcess(process_);
137}
138
Alex Moshchuk8e5c1952019-01-15 03:39:50139// static
140BrowsingInstanceId SiteInstanceImpl::NextBrowsingInstanceId() {
141 return BrowsingInstance::NextBrowsingInstanceId();
142}
143
144bool SiteInstanceImpl::HasProcess() {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28145 if (process_ != nullptr)
[email protected]41fb79a52012-06-29 16:34:33146 return true;
147
148 // If we would use process-per-site for this site, also check if there is an
149 // existing process that we would use if GetProcess() were called.
Alex Moshchukf7488792019-03-11 22:37:57150 BrowserContext* browser_context = browsing_instance_->GetBrowserContext();
[email protected]41fb79a52012-06-29 16:34:33151 if (has_site_ &&
[email protected]1ae93fb12013-06-14 03:38:56152 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_) &&
Alex Moshchuk8e5c1952019-01-15 03:39:50153 RenderProcessHostImpl::GetSoleProcessHostForSite(
154 browser_context, GetIsolationContext(), site_, lock_url_)) {
[email protected]41fb79a52012-06-29 16:34:33155 return true;
156 }
157
158 return false;
[email protected]5ab79b02010-04-26 16:47:11159}
160
[email protected]41fb79a52012-06-29 16:34:33161RenderProcessHost* SiteInstanceImpl::GetProcess() {
[email protected]08c8ec7e2010-07-11 17:14:48162 // TODO(erikkay) It would be nice to ensure that the renderer type had been
163 // properly set before we get here. The default tab creation case winds up
164 // with no site set at this point, so it will default to TYPE_NORMAL. This
165 // may not be correct, so we'll wind up potentially creating a process that
166 // we then throw away, or worse sharing a process with the wrong process type.
167 // See crbug.com/43448.
168
initial.commit09911bf2008-07-26 23:55:29169 // Create a new process if ours went away or was reused.
[email protected]4566f132009-03-12 01:55:13170 if (!process_) {
Alex Moshchukf7488792019-03-11 22:37:57171 BrowserContext* browser_context = browsing_instance_->GetBrowserContext();
[email protected]41fb79a52012-06-29 16:34:33172
clamy63960c32017-05-10 22:57:07173 // Check if the ProcessReusePolicy should be updated.
174 bool should_use_process_per_site =
creisa9f04ba2017-05-19 22:27:42175 has_site_ &&
176 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_);
clamy63960c32017-05-10 22:57:07177 if (should_use_process_per_site) {
178 process_reuse_policy_ = ProcessReusePolicy::PROCESS_PER_SITE;
179 } else if (process_reuse_policy_ == ProcessReusePolicy::PROCESS_PER_SITE) {
180 process_reuse_policy_ = ProcessReusePolicy::DEFAULT;
[email protected]41fb79a52012-06-29 16:34:33181 }
182
Alex Moshchuk5f926a52018-08-29 20:57:30183 process_ = RenderProcessHostImpl::GetProcessHostForSiteInstance(this);
avi6d686db2017-01-06 02:28:57184
[email protected]41fb79a52012-06-29 16:34:33185 CHECK(process_);
[email protected]40bfaf32013-11-19 01:35:43186 process_->AddObserver(this);
[email protected]41fb79a52012-06-29 16:34:33187
Nate Chapin71da03c2019-02-05 01:21:41188 MaybeSetBrowsingInstanceDefaultProcess();
189
[email protected]41fb79a52012-06-29 16:34:33190 // If we are using process-per-site, we need to register this process
191 // for the current site so that we can find it again. (If no site is set
192 // at this time, we will register it in SetSite().)
clamy63960c32017-05-10 22:57:07193 if (process_reuse_policy_ == ProcessReusePolicy::PROCESS_PER_SITE &&
194 has_site_) {
Alex Moshchuk5f926a52018-08-29 20:57:30195 RenderProcessHostImpl::RegisterSoleProcessHostForSite(browser_context,
196 process_, this);
[email protected]41fb79a52012-06-29 16:34:33197 }
initial.commit09911bf2008-07-26 23:55:29198
naskob8744d22014-08-28 17:07:43199 TRACE_EVENT2("navigation", "SiteInstanceImpl::GetProcess",
200 "site id", id_, "process id", process_->GetID());
[email protected]46488322012-10-30 03:22:20201 GetContentClient()->browser()->SiteInstanceGotProcess(this);
[email protected]6f371442011-11-09 06:45:46202
[email protected]313b80bd2011-11-23 03:49:10203 if (has_site_)
alexmos13fe1962017-06-28 04:25:12204 LockToOriginIfNeeded();
initial.commit09911bf2008-07-26 23:55:29205 }
[email protected]4566f132009-03-12 01:55:13206 DCHECK(process_);
initial.commit09911bf2008-07-26 23:55:29207
[email protected]4566f132009-03-12 01:55:13208 return process_;
initial.commit09911bf2008-07-26 23:55:29209}
210
Lukasz Anforowicz3caa8372018-06-05 17:22:07211bool SiteInstanceImpl::CanAssociateWithSpareProcess() {
212 return can_associate_with_spare_process_;
213}
214
215void SiteInstanceImpl::PreventAssociationWithSpareProcess() {
216 can_associate_with_spare_process_ = false;
217}
218
[email protected]b6583592012-01-25 19:52:33219void SiteInstanceImpl::SetSite(const GURL& url) {
naskob8744d22014-08-28 17:07:43220 TRACE_EVENT2("navigation", "SiteInstanceImpl::SetSite",
221 "site id", id_, "url", url.possibly_invalid_spec());
initial.commit09911bf2008-07-26 23:55:29222 // A SiteInstance's site should not change.
223 // TODO(creis): When following links or script navigations, we can currently
224 // render pages from other sites in this SiteInstance. This will eventually
225 // be fixed, but until then, we should still not set the site of a
226 // SiteInstance more than once.
227 DCHECK(!has_site_);
228
229 // Remember that this SiteInstance has been used to load a URL, even if the
230 // URL is invalid.
231 has_site_ = true;
Alex Moshchukf7488792019-03-11 22:37:57232 BrowserContext* browser_context = browsing_instance_->GetBrowserContext();
Alex Moshchuk25c64bb2017-12-02 02:50:11233 original_url_ = url;
Aaron Colwellddeccbdb2019-03-08 01:11:03234 browsing_instance_->GetSiteAndLockForURL(
235 url, /* allow_default_instance */ false, &site_, &lock_url_);
initial.commit09911bf2008-07-26 23:55:29236
237 // Now that we have a site, register it with the BrowsingInstance. This
238 // ensures that we won't create another SiteInstance for this site within
239 // the same BrowsingInstance, because all same-site pages within a
240 // BrowsingInstance can script each other.
241 browsing_instance_->RegisterSiteInstance(this);
[email protected]313b80bd2011-11-23 03:49:10242
clamy63960c32017-05-10 22:57:07243 // Update the process reuse policy based on the site.
244 bool should_use_process_per_site =
245 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_);
246 if (should_use_process_per_site) {
247 process_reuse_policy_ = ProcessReusePolicy::PROCESS_PER_SITE;
248 }
249
[email protected]41fb79a52012-06-29 16:34:33250 if (process_) {
alexmos13fe1962017-06-28 04:25:12251 LockToOriginIfNeeded();
[email protected]41fb79a52012-06-29 16:34:33252
253 // Ensure the process is registered for this site if necessary.
clamy63960c32017-05-10 22:57:07254 if (should_use_process_per_site) {
Alex Moshchuk5f926a52018-08-29 20:57:30255 RenderProcessHostImpl::RegisterSoleProcessHostForSite(browser_context,
256 process_, this);
[email protected]41fb79a52012-06-29 16:34:33257 }
Nate Chapin71da03c2019-02-05 01:21:41258 MaybeSetBrowsingInstanceDefaultProcess();
[email protected]41fb79a52012-06-29 16:34:33259 }
initial.commit09911bf2008-07-26 23:55:29260}
261
[email protected]77ab17312012-09-28 15:34:59262const GURL& SiteInstanceImpl::GetSiteURL() const {
[email protected]b6583592012-01-25 19:52:33263 return site_;
264}
265
266bool SiteInstanceImpl::HasSite() const {
267 return has_site_;
268}
269
270bool SiteInstanceImpl::HasRelatedSiteInstance(const GURL& url) {
initial.commit09911bf2008-07-26 23:55:29271 return browsing_instance_->HasSiteInstance(url);
272}
273
dchengbccd6b82016-03-30 16:24:19274scoped_refptr<SiteInstance> SiteInstanceImpl::GetRelatedSiteInstance(
275 const GURL& url) {
Aaron Colwellddeccbdb2019-03-08 01:11:03276 return browsing_instance_->GetSiteInstanceForURL(
277 url, /* allow_default_instance */ true);
initial.commit09911bf2008-07-26 23:55:29278}
279
[email protected]14392a52012-05-02 20:28:44280bool SiteInstanceImpl::IsRelatedSiteInstance(const SiteInstance* instance) {
[email protected]fc72bb12013-06-02 21:13:46281 return browsing_instance_.get() == static_cast<const SiteInstanceImpl*>(
282 instance)->browsing_instance_.get();
[email protected]14392a52012-05-02 20:28:44283}
284
[email protected]d5072a82014-05-15 05:50:18285size_t SiteInstanceImpl::GetRelatedActiveContentsCount() {
286 return browsing_instance_->active_contents_count();
287}
288
[email protected]f88628d02012-11-11 17:58:59289bool SiteInstanceImpl::HasWrongProcessForURL(const GURL& url) {
[email protected]d292d8a2011-05-25 03:47:11290 // Having no process isn't a problem, since we'll assign it correctly.
[email protected]f88628d02012-11-11 17:58:59291 // Note that HasProcess() may return true if process_ is null, in
292 // process-per-site cases where there's an existing process available.
293 // We want to use such a process in the IsSuitableHost check, so we
294 // may end up assigning process_ in the GetProcess() call below.
295 if (!HasProcess())
[email protected]d292d8a2011-05-25 03:47:11296 return false;
297
[email protected]144a8102012-01-14 01:05:31298 // If the URL to navigate to can be associated with any site instance,
299 // we want to keep it in the same process.
[email protected]c02f1ba2014-02-03 06:53:53300 if (IsRendererDebugURL(url))
[email protected]144a8102012-01-14 01:05:31301 return false;
302
Nasko Oskov978c16b2018-08-03 22:17:06303 // Any process can host an about:blank URL, except the one used for error
304 // pages, which should not commit successful navigations. This check avoids a
305 // process transfer for browser-initiated navigations to about:blank in a
306 // dedicated process; without it, IsSuitableHost would consider this process
307 // unsuitable for about:blank when it compares origin locks.
308 // Renderer-initiated navigations will handle about:blank navigations
309 // elsewhere and leave them in the source SiteInstance, along with
310 // about:srcdoc and data:.
311 if (url.IsAboutBlank() && site_ != GURL(kUnreachableWebDataURL))
alexmos13fe1962017-06-28 04:25:12312 return false;
313
[email protected]88aae972011-12-16 01:14:18314 // If the site URL is an extension (e.g., for hosted apps or WebUI) but the
[email protected]d292d8a2011-05-25 03:47:11315 // process is not (or vice versa), make sure we notice and fix it.
Aaron Colwellddeccbdb2019-03-08 01:11:03316 GURL site_url;
317 GURL origin_lock;
318
319 // Note: This call must return information that is identical to what
320 // would be reported in the SiteInstance returned by
321 // GetRelatedSiteInstance(url).
322 browsing_instance_->GetSiteAndLockForURL(
323 url, /* allow_default_instance */ true, &site_url, &origin_lock);
[email protected]88aae972011-12-16 01:14:18324 return !RenderProcessHostImpl::IsSuitableHost(
Alex Moshchukf7488792019-03-11 22:37:57325 GetProcess(), browsing_instance_->GetBrowserContext(),
Alex Moshchuk8e5c1952019-01-15 03:39:50326 GetIsolationContext(), site_url, origin_lock);
[email protected]d292d8a2011-05-25 03:47:11327}
328
nickd30fd962015-07-27 21:51:08329bool SiteInstanceImpl::RequiresDedicatedProcess() {
330 if (!has_site_)
331 return false;
nickd5bbd0b2016-03-31 19:52:44332
Lukasz Anforowicz1a0a89a2019-03-29 22:31:02333 return DoesSiteRequireDedicatedProcess(GetIsolationContext(), site_);
nickd30fd962015-07-27 21:51:08334}
335
nick9f34e2892016-01-12 21:01:07336void SiteInstanceImpl::IncrementActiveFrameCount() {
337 active_frame_count_++;
338}
339
340void SiteInstanceImpl::DecrementActiveFrameCount() {
ericwilligers254597b2016-10-17 10:32:31341 if (--active_frame_count_ == 0) {
342 for (auto& observer : observers_)
343 observer.ActiveFrameCountIsZero(this);
344 }
nick9f34e2892016-01-12 21:01:07345}
346
[email protected]d5072a82014-05-15 05:50:18347void SiteInstanceImpl::IncrementRelatedActiveContentsCount() {
348 browsing_instance_->increment_active_contents_count();
349}
350
351void SiteInstanceImpl::DecrementRelatedActiveContentsCount() {
352 browsing_instance_->decrement_active_contents_count();
353}
354
nick9f34e2892016-01-12 21:01:07355void SiteInstanceImpl::AddObserver(Observer* observer) {
356 observers_.AddObserver(observer);
357}
358
359void SiteInstanceImpl::RemoveObserver(Observer* observer) {
360 observers_.RemoveObserver(observer);
361}
362
[email protected]4c3a23582012-08-18 08:54:34363BrowserContext* SiteInstanceImpl::GetBrowserContext() const {
Alex Moshchukf7488792019-03-11 22:37:57364 return browsing_instance_->GetBrowserContext();
[email protected]72daaa92012-01-18 13:39:02365}
366
nickcc0d9142015-10-14 16:27:10367// static
dchengbccd6b82016-03-30 16:24:19368scoped_refptr<SiteInstance> SiteInstance::Create(
369 BrowserContext* browser_context) {
Lukasz Anforowicz4726a172018-10-15 21:25:10370 DCHECK(browser_context);
dchengbccd6b82016-03-30 16:24:19371 return SiteInstanceImpl::Create(browser_context);
initial.commit09911bf2008-07-26 23:55:29372}
373
nickcc0d9142015-10-14 16:27:10374// static
dchengbccd6b82016-03-30 16:24:19375scoped_refptr<SiteInstance> SiteInstance::CreateForURL(
376 BrowserContext* browser_context,
377 const GURL& url) {
Lukasz Anforowicz4726a172018-10-15 21:25:10378 DCHECK(browser_context);
dchengbccd6b82016-03-30 16:24:19379 return SiteInstanceImpl::CreateForURL(browser_context, url);
[email protected]d8a96622009-05-07 19:21:16380}
381
nickcc0d9142015-10-14 16:27:10382// static
Nasko Oskovdeb6c7ea2017-11-30 18:18:11383bool SiteInstance::ShouldAssignSiteForURL(const GURL& url) {
384 return SiteInstanceImpl::ShouldAssignSiteForURL(url);
385}
386
Alex Moshchuk78cf66bda2018-11-30 01:49:30387bool SiteInstanceImpl::IsSameSiteWithURL(const GURL& url) {
388 return SiteInstanceImpl::IsSameWebSite(
Lukasz Anforowicz1a0a89a2019-03-29 22:31:02389 GetIsolationContext(), site_, url,
390 true /* should_compare_effective_urls */);
Alex Moshchuk25c64bb2017-12-02 02:50:11391}
392
Alex Moshchuk78cf66bda2018-11-30 01:49:30393// static
Lukasz Anforowicz1a0a89a2019-03-29 22:31:02394bool SiteInstanceImpl::IsSameWebSite(const IsolationContext& isolation_context,
Alex Moshchuk25c64bb2017-12-02 02:50:11395 const GURL& real_src_url,
396 const GURL& real_dest_url,
397 bool should_compare_effective_urls) {
Lukasz Anforowicz1a0a89a2019-03-29 22:31:02398 DCHECK_CURRENTLY_ON(BrowserThread::UI);
399 BrowserContext* browser_context =
400 isolation_context.browser_or_resource_context().ToBrowserContext();
Lukasz Anforowicz4726a172018-10-15 21:25:10401 DCHECK(browser_context);
402
Alex Moshchuk25c64bb2017-12-02 02:50:11403 GURL src_url =
404 should_compare_effective_urls
405 ? SiteInstanceImpl::GetEffectiveURL(browser_context, real_src_url)
406 : real_src_url;
407 GURL dest_url =
408 should_compare_effective_urls
409 ? SiteInstanceImpl::GetEffectiveURL(browser_context, real_dest_url)
410 : real_dest_url;
[email protected]399583b2012-12-11 09:33:42411
412 // We infer web site boundaries based on the registered domain name of the
413 // top-level page and the scheme. We do not pay attention to the port if
414 // one is present, because pages served from different ports can still
415 // access each other if they change their document.domain variable.
416
417 // Some special URLs will match the site instance of any other URL. This is
418 // done before checking both of them for validity, since we want these URLs
419 // to have the same site instance as even an invalid one.
[email protected]855d7d572014-08-02 11:18:17420 if (IsRendererDebugURL(src_url) || IsRendererDebugURL(dest_url))
[email protected]399583b2012-12-11 09:33:42421 return true;
422
423 // If either URL is invalid, they aren't part of the same site.
[email protected]855d7d572014-08-02 11:18:17424 if (!src_url.is_valid() || !dest_url.is_valid())
[email protected]399583b2012-12-11 09:33:42425 return false;
426
[email protected]855d7d572014-08-02 11:18:17427 // If the destination url is just a blank page, we treat them as part of the
428 // same site.
429 GURL blank_page(url::kAboutBlankURL);
430 if (dest_url == blank_page)
431 return true;
432
Alex Moshchuka308c9b2018-02-08 20:58:14433 // If the source and destination URLs are equal excluding the hash, they have
434 // the same site. This matters for file URLs, where SameDomainOrHost() would
435 // otherwise return false below.
436 if (src_url.EqualsIgnoringRef(dest_url))
437 return true;
438
Daniel Cheng88186bd52017-10-20 08:14:46439 url::Origin src_origin = url::Origin::Create(src_url);
440 url::Origin dest_origin = url::Origin::Create(dest_url);
alexmos3b9ad102017-05-26 23:41:08441
[email protected]399583b2012-12-11 09:33:42442 // If the schemes differ, they aren't part of the same site.
alexmoscbf995782017-06-01 03:13:13443 if (src_origin.scheme() != dest_origin.scheme())
[email protected]399583b2012-12-11 09:33:42444 return false;
445
alexmos4bc26322017-07-01 00:57:14446 if (!net::registry_controlled_domains::SameDomainOrHost(
447 src_origin, dest_origin,
448 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) {
449 return false;
450 }
451
452 // If the sites are the same, check isolated origins. If either URL matches
Alex Moshchuk4e19b362018-09-10 21:14:36453 // an isolated origin, compare origins rather than sites. As an optimization
454 // to avoid unneeded isolated origin lookups, shortcut this check if the two
455 // origins are the same.
456 if (src_origin == dest_origin)
457 return true;
alexmos4bc26322017-07-01 00:57:14458 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
459 url::Origin src_isolated_origin;
460 url::Origin dest_isolated_origin;
Alex Moshchuk8e5c1952019-01-15 03:39:50461 bool src_origin_is_isolated = policy->GetMatchingIsolatedOrigin(
462 isolation_context, src_origin, &src_isolated_origin);
463 bool dest_origin_is_isolated = policy->GetMatchingIsolatedOrigin(
464 isolation_context, dest_origin, &dest_isolated_origin);
alexmos4bc26322017-07-01 00:57:14465 if (src_origin_is_isolated || dest_origin_is_isolated) {
466 // Compare most specific matching origins to ensure that a subdomain of an
467 // isolated origin (e.g., https://subdomain.isolated.foo.com) also matches
468 // the isolated origin's site URL (e.g., https://isolated.foo.com).
469 return src_isolated_origin == dest_isolated_origin;
470 }
471
472 return true;
[email protected]399583b2012-12-11 09:33:42473}
474
nickcc0d9142015-10-14 16:27:10475// static
[email protected]399583b2012-12-11 09:33:42476GURL SiteInstance::GetSiteForURL(BrowserContext* browser_context,
Alex Moshchuk5f926a52018-08-29 20:57:30477 const GURL& url) {
Aaron Colwellea6921f2019-01-29 16:50:39478 DCHECK_CURRENTLY_ON(BrowserThread::UI);
479 DCHECK(browser_context);
480
Alex Moshchuk5f926a52018-08-29 20:57:30481 // By default, GetSiteForURL will resolve |real_url| to an effective URL
Alex Moshchuk8e5c1952019-01-15 03:39:50482 // before computing its site, so set |should_use_effective_urls| to true.
483 //
484 // TODO(alexmos): Callers inside content/ should already be using the
485 // internal SiteInstanceImpl version and providing a proper IsolationContext.
486 // For callers outside content/, plumb the applicable IsolationContext here,
487 // where needed. Eventually, GetSiteForURL should always require an
488 // IsolationContext to be passed in, and this implementation should just
489 // become SiteInstanceImpl::GetSiteForURL.
Aaron Colwellea6921f2019-01-29 16:50:39490 return SiteInstanceImpl::GetSiteForURL(
Alex Moshchuk99b795422019-03-07 00:27:32491 IsolationContext(browser_context), url,
Aaron Colwellea6921f2019-01-29 16:50:39492 true /* should_use_effective_urls */);
Alex Moshchuk5f926a52018-08-29 20:57:30493}
494
495// static
Alex Moshchuk8e5c1952019-01-15 03:39:50496GURL SiteInstanceImpl::DetermineProcessLockURL(
Alex Moshchuk8e5c1952019-01-15 03:39:50497 const IsolationContext& isolation_context,
498 const GURL& url) {
Alex Moshchuk5f926a52018-08-29 20:57:30499 // For the process lock URL, convert |url| to a site without resolving |url|
500 // to an effective URL.
Alex Moshchukf7488792019-03-11 22:37:57501 return SiteInstanceImpl::GetSiteForURL(isolation_context, url,
Alex Moshchuk5f926a52018-08-29 20:57:30502 false /* should_use_effective_urls */);
503}
504
Aaron Colwellea6921f2019-01-29 16:50:39505// static
Alex Moshchukf7488792019-03-11 22:37:57506GURL SiteInstanceImpl::GetSiteForURL(const IsolationContext& isolation_context,
Alex Moshchuk5f926a52018-08-29 20:57:30507 const GURL& real_url,
508 bool should_use_effective_urls) {
[email protected]25a9e8e532012-06-22 06:16:28509 // TODO(fsamuel, creis): For some reason appID is not recognized as a host.
[email protected]6eb1a11e2013-10-09 00:54:37510 if (real_url.SchemeIs(kGuestScheme))
[email protected]25a9e8e532012-06-22 06:16:28511 return real_url;
512
Aaron Colwellea6921f2019-01-29 16:50:39513 if (should_use_effective_urls)
514 DCHECK_CURRENTLY_ON(BrowserThread::UI);
515
Alex Moshchuk5f926a52018-08-29 20:57:30516 GURL url = should_use_effective_urls
Alex Moshchukf7488792019-03-11 22:37:57517 ? SiteInstanceImpl::GetEffectiveURL(
518 isolation_context.browser_or_resource_context()
519 .ToBrowserContext(),
520 real_url)
Alex Moshchuk5f926a52018-08-29 20:57:30521 : real_url;
Daniel Cheng88186bd52017-10-20 08:14:46522 url::Origin origin = url::Origin::Create(url);
[email protected]3a8eecb2010-04-22 23:56:30523
Lukasz Anforowicz48097c42017-12-15 00:23:38524 // If the url has a host, then determine the site. Skip file URLs to avoid a
525 // situation where site URL of file://localhost/ would mismatch Blink's origin
526 // (which ignores the hostname in this case - see https://crbug.com/776160).
527 if (!origin.host().empty() && origin.scheme() != url::kFileScheme) {
Alex Moshchuk4e19b362018-09-10 21:14:36528 GURL site_url(GetSiteForOrigin(origin));
529
530 // Isolated origins should use the full origin as their site URL. A
531 // subdomain of an isolated origin should also use that isolated origin's
532 // site URL. It is important to check |origin| (based on |url|) rather than
533 // |real_url| here, since some effective URLs (such as for NTP) need to be
534 // resolved prior to the isolated origin lookup.
535 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
536 url::Origin isolated_origin;
Alex Moshchuk8e5c1952019-01-15 03:39:50537 if (policy->GetMatchingIsolatedOrigin(isolation_context, origin, site_url,
538 &isolated_origin))
Alex Moshchuk4e19b362018-09-10 21:14:36539 return isolated_origin.GetURL();
Alex Moshchuk5f926a52018-08-29 20:57:30540
541 // If an effective URL was used, augment the effective site URL with the
542 // underlying web site in the hash. This is needed to keep
543 // navigations across sites covered by one hosted app in separate
544 // SiteInstances. See https://crbug.com/791796.
545 //
546 // TODO(https://crbug.com/734722): Consider replacing this hack with
547 // a proper security principal.
548 if (should_use_effective_urls && url != real_url) {
Alex Moshchuk4e19b362018-09-10 21:14:36549 std::string non_translated_site_url(
Alex Moshchukf7488792019-03-11 22:37:57550 GetSiteForURL(isolation_context, real_url,
Alex Moshchuk4e19b362018-09-10 21:14:36551 false /* should_use_effective_urls */)
552 .spec());
553 GURL::Replacements replacements;
554 replacements.SetRefStr(non_translated_site_url.c_str());
555 site_url = site_url.ReplaceComponents(replacements);
Alex Moshchuk5f926a52018-08-29 20:57:30556 }
557
Alex Moshchuk4e19b362018-09-10 21:14:36558 return site_url;
initial.commit09911bf2008-07-26 23:55:29559 }
creisf60c2cd2014-12-18 00:41:02560
561 // If there is no host but there is a scheme, return the scheme.
562 // This is useful for cases like file URLs.
Chris Palmerab5e5b52018-09-28 19:19:30563 if (!origin.opaque()) {
Lukasz Anforowicz217fd272018-03-07 21:41:43564 // Prefer to use the scheme of |origin| rather than |url|, to correctly
Alex Moshchukb1f87482018-07-19 01:51:51565 // cover blob:file: and filesystem:file: URIs (see also
566 // https://crbug.com/697111).
Lukasz Anforowiczd926a842018-03-09 01:50:45567 DCHECK(!origin.scheme().empty());
568 return GURL(origin.scheme() + ":");
569 } else if (url.has_scheme()) {
Alex Moshchukb1f87482018-07-19 01:51:51570 // In some cases, it is not safe to use just the scheme as a site URL, as
Charlie Reis0bb3f5c72018-08-06 22:46:01571 // that might allow two URLs created by different sites to share a process.
572 // See https://crbug.com/863623 and https://crbug.com/863069.
Alex Moshchukb1f87482018-07-19 01:51:51573 //
574 // TODO(alexmos,creis): This should eventually be expanded to certain other
Charlie Reis0bb3f5c72018-08-06 22:46:01575 // schemes, such as file:.
576 // TODO(creis): This currently causes problems with tests on Android and
577 // Android WebView. For now, skip it when Site Isolation is not enabled,
578 // since there's no need to isolate data and blob URLs from each other in
579 // that case.
580 bool is_site_isolation_enabled =
581 SiteIsolationPolicy::UseDedicatedProcessesForAllSites() ||
582 SiteIsolationPolicy::AreIsolatedOriginsEnabled();
583 if (is_site_isolation_enabled &&
584 (url.SchemeIsBlob() || url.scheme() == url::kDataScheme)) {
Alex Moshchukb1f87482018-07-19 01:51:51585 // We get here for blob URLs of form blob:null/guid. Use the full URL
586 // with the guid in that case, which isolates all blob URLs with unique
Charlie Reis0bb3f5c72018-08-06 22:46:01587 // origins from each other. We also get here for browser-initiated
588 // navigations to data URLs, which have a unique origin and should only
589 // share a process when they are identical. Remove hash from the URL in
590 // either case, since same-document navigations shouldn't use a different
591 // site URL.
Alex Moshchukb1f87482018-07-19 01:51:51592 if (url.has_ref()) {
593 GURL::Replacements replacements;
594 replacements.ClearRef();
595 url = url.ReplaceComponents(replacements);
596 }
597 return url;
598 }
599
Lukasz Anforowiczd926a842018-03-09 01:50:45600 DCHECK(!url.scheme().empty());
creisf60c2cd2014-12-18 00:41:02601 return GURL(url.scheme() + ":");
Lukasz Anforowiczd926a842018-03-09 01:50:45602 }
creisf60c2cd2014-12-18 00:41:02603
604 // Otherwise the URL should be invalid; return an empty site.
Lukasz Anforowicz217fd272018-03-07 21:41:43605 DCHECK(!url.is_valid()) << url;
creisf60c2cd2014-12-18 00:41:02606 return GURL();
initial.commit09911bf2008-07-26 23:55:29607}
608
nickcc0d9142015-10-14 16:27:10609// static
Alex Moshchuk4e19b362018-09-10 21:14:36610GURL SiteInstanceImpl::GetSiteForOrigin(const url::Origin& origin) {
611 // Only keep the scheme and registered domain of |origin|.
612 std::string domain = net::registry_controlled_domains::GetDomainAndRegistry(
613 origin.host(),
614 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
615 std::string site = origin.scheme();
616 site += url::kStandardSchemeSeparator;
617 site += domain.empty() ? origin.host() : domain;
618 return GURL(site);
619}
620
621// static
[email protected]4c3a23582012-08-18 08:54:34622GURL SiteInstanceImpl::GetEffectiveURL(BrowserContext* browser_context,
623 const GURL& url) {
Aaron Colwellea6921f2019-01-29 16:50:39624 DCHECK(browser_context);
Alex Moshchuka31c7882018-01-17 00:57:30625 return GetContentClient()->browser()->GetEffectiveURL(browser_context, url);
[email protected]3a8eecb2010-04-22 23:56:30626}
627
nickcc0d9142015-10-14 16:27:10628// static
Alex Moshchuk25c64bb2017-12-02 02:50:11629bool SiteInstanceImpl::HasEffectiveURL(BrowserContext* browser_context,
630 const GURL& url) {
631 return GetEffectiveURL(browser_context, url) != url;
632}
633
634// static
nickcc0d9142015-10-14 16:27:10635bool SiteInstanceImpl::DoesSiteRequireDedicatedProcess(
Alex Moshchuk8e5c1952019-01-15 03:39:50636 const IsolationContext& isolation_context,
nickdb193a12016-09-09 23:09:23637 const GURL& url) {
Lukasz Anforowicz4aff3b82019-04-04 16:00:33638 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO) ||
639 BrowserThread::CurrentlyOn(BrowserThread::UI));
640 DCHECK(isolation_context.browser_or_resource_context());
Lukasz Anforowicz4726a172018-10-15 21:25:10641
nickcc0d9142015-10-14 16:27:10642 // If --site-per-process is enabled, site isolation is enabled everywhere.
643 if (SiteIsolationPolicy::UseDedicatedProcessesForAllSites())
644 return true;
645
alexmos3b9ad102017-05-26 23:41:08646 // Always require a dedicated process for isolated origins.
Alex Moshchukf7488792019-03-11 22:37:57647 GURL site_url = SiteInstanceImpl::GetSiteForURL(
648 isolation_context, url, true /* should_compare_effective_urls */);
alexmos3b9ad102017-05-26 23:41:08649 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
Alex Moshchuk8e5c1952019-01-15 03:39:50650 if (policy->IsIsolatedOrigin(isolation_context,
651 url::Origin::Create(site_url)))
alexmos3b9ad102017-05-26 23:41:08652 return true;
653
Lukasz Anforowicz1f5ad402018-09-28 23:57:54654 // Error pages in main frames do require isolation, however since this is
655 // missing the context whether this is for a main frame or not, that part
656 // is enforced in RenderFrameHostManager.
657 if (site_url.SchemeIs(kChromeErrorScheme))
658 return true;
659
660 // Isolate kChromeUIScheme pages from one another and from other kinds of
661 // schemes.
662 if (site_url.SchemeIs(content::kChromeUIScheme))
663 return true;
664
nickdb193a12016-09-09 23:09:23665 // Let the content embedder enable site isolation for specific URLs. Use the
666 // canonical site url for this check, so that schemes with nested origins
667 // (blob and filesystem) work properly.
Avi Drissman8c36df72017-12-11 21:19:20668 if (GetContentClient()->browser()->DoesSiteRequireDedicatedProcess(
Lukasz Anforowicz4aff3b82019-04-04 16:00:33669 isolation_context.browser_or_resource_context(), site_url)) {
nickcc0d9142015-10-14 16:27:10670 return true;
671 }
672
673 return false;
674}
675
alexmos13fe1962017-06-28 04:25:12676// static
Alex Moshchuk8e5c1952019-01-15 03:39:50677bool SiteInstanceImpl::ShouldLockToOrigin(
Alex Moshchuk8e5c1952019-01-15 03:39:50678 const IsolationContext& isolation_context,
679 GURL site_url) {
Lukasz Anforowicz1a0a89a2019-03-29 22:31:02680 DCHECK_CURRENTLY_ON(BrowserThread::UI);
681 BrowserContext* browser_context =
682 isolation_context.browser_or_resource_context().ToBrowserContext();
Lukasz Anforowicz4726a172018-10-15 21:25:10683 DCHECK(browser_context);
684
creis0b49fa12017-07-10 16:31:11685 // Don't lock to origin in --single-process mode, since this mode puts
686 // cross-site pages into the same process.
Alex Moshchukd4c4ca32018-08-15 20:36:51687 if (RenderProcessHost::run_renderer_in_process())
creis0b49fa12017-07-10 16:31:11688 return false;
689
Lukasz Anforowicz1a0a89a2019-03-29 22:31:02690 if (!DoesSiteRequireDedicatedProcess(isolation_context, site_url))
alexmos13fe1962017-06-28 04:25:12691 return false;
692
693 // Guest processes cannot be locked to their site because guests always have
694 // a fixed SiteInstance. The site of GURLs a guest loads doesn't match that
695 // SiteInstance. So we skip locking the guest process to the site.
696 // TODO(ncarter): Remove this exclusion once we can make origin lock per
697 // RenderFrame routing id.
698 if (site_url.SchemeIs(content::kGuestScheme))
699 return false;
700
alexmos13fe1962017-06-28 04:25:12701 // TODO(creis, nick): Until we can handle sites with effective URLs at the
702 // call sites of ChildProcessSecurityPolicy::CanAccessDataForOrigin, we
703 // must give the embedder a chance to exempt some sites to avoid process
704 // kills.
705 if (!GetContentClient()->browser()->ShouldLockToOrigin(browser_context,
706 site_url)) {
707 return false;
708 }
709
710 return true;
711}
712
Lukasz Anforowicz27851e62018-12-18 23:04:08713// static
714base::Optional<url::Origin> SiteInstanceImpl::GetRequestInitiatorSiteLock(
Lukasz Anforowicz27851e62018-12-18 23:04:08715 GURL site_url) {
Lukasz Anforowicz27851e62018-12-18 23:04:08716 // The following schemes are safe for sites that require a process lock:
717 // - data: - locking |request_initiator| to an opaque origin
718 // - http/https - requiring |request_initiator| to match |site_url| with
719 // DomainIs (i.e. suffix-based) comparison.
720 if (site_url.SchemeIsHTTPOrHTTPS() || site_url.SchemeIs(url::kDataScheme))
721 return url::Origin::Create(site_url);
722
723 // Other schemes might not be safe to use as |request_initiator_site_lock|.
724 // One example is chrome-guest://...
725 return base::nullopt;
726}
727
[email protected]40bfaf32013-11-19 01:35:43728void SiteInstanceImpl::RenderProcessHostDestroyed(RenderProcessHost* host) {
729 DCHECK_EQ(process_, host);
730 process_->RemoveObserver(this);
nickd5bbd0b2016-03-31 19:52:44731 process_ = nullptr;
[email protected]4566f132009-03-12 01:55:13732}
[email protected]313b80bd2011-11-23 03:49:10733
Bo Liu2a489402018-04-24 23:41:27734void SiteInstanceImpl::RenderProcessExited(
735 RenderProcessHost* host,
736 const ChildProcessTerminationInfo& info) {
ericwilligers254597b2016-10-17 10:32:31737 for (auto& observer : observers_)
738 observer.RenderProcessGone(this);
nick9f34e2892016-01-12 21:01:07739}
740
alexmos13fe1962017-06-28 04:25:12741void SiteInstanceImpl::LockToOriginIfNeeded() {
742 DCHECK(HasSite());
743
744 // From now on, this process should be considered "tainted" for future
745 // process reuse decisions:
746 // (1) If |site_| required a dedicated process, this SiteInstance's process
747 // can only host URLs for the same site.
748 // (2) Even if |site_| does not require a dedicated process, this
749 // SiteInstance's process still cannot be reused to host other sites
750 // requiring dedicated sites in the future.
751 // We can get here either when we commit a URL into a SiteInstance that does
752 // not yet have a site, or when we create a process for a SiteInstance with a
753 // preassigned site.
754 process_->SetIsUsed();
755
Alex Moshchuk47dd00f2017-10-10 17:15:23756 ChildProcessSecurityPolicyImpl* policy =
757 ChildProcessSecurityPolicyImpl::GetInstance();
Alex Moshchuk5f926a52018-08-29 20:57:30758 auto lock_state = policy->CheckOriginLock(process_->GetID(), lock_url());
Lukasz Anforowicz1a0a89a2019-03-29 22:31:02759 if (ShouldLockToOrigin(GetIsolationContext(), site_)) {
Alex Moshchuk9cf5a45d2017-08-14 18:50:57760 // Sanity check that this won't try to assign an origin lock to a <webview>
761 // process, which can't be locked.
762 CHECK(!process_->IsForGuestsOnly());
763
Alex Moshchuk9cf5a45d2017-08-14 18:50:57764 switch (lock_state) {
Alex Moshchuk1656c672018-04-28 01:48:34765 case CheckOriginLockResult::NO_LOCK: {
Alex Moshchuk47dd00f2017-10-10 17:15:23766 // TODO(nick): When all sites are isolated, this operation provides
767 // strong protection. If only some sites are isolated, we need
768 // additional logic to prevent the non-isolated sites from requesting
769 // resources for isolated sites. https://crbug.com/509125
Nasko Oskov866f86cc2018-08-18 04:11:11770 TRACE_EVENT2("navigation", "SiteInstanceImpl::LockToOrigin", "site id",
Alex Moshchuk5f926a52018-08-29 20:57:30771 id_, "lock", lock_url().possibly_invalid_spec());
Alex Moshchuk8e5c1952019-01-15 03:39:50772 process_->LockToOrigin(GetIsolationContext(), lock_url());
Alex Moshchuk9cf5a45d2017-08-14 18:50:57773 break;
774 }
Alex Moshchuk1656c672018-04-28 01:48:34775 case CheckOriginLockResult::HAS_WRONG_LOCK:
Alex Moshchuk9cf5a45d2017-08-14 18:50:57776 // We should never attempt to reassign a different origin lock to a
777 // process.
Robert Sesek1e07e372017-12-09 01:34:42778 base::debug::SetCrashKeyString(bad_message::GetRequestedSiteURLKey(),
779 site_.spec());
780 base::debug::SetCrashKeyString(
781 bad_message::GetKilledProcessOriginLockKey(),
Alex Moshchuk4479b97c2017-10-17 23:20:32782 policy->GetOriginLock(process_->GetID()).spec());
Alex Moshchuk5f926a52018-08-29 20:57:30783 CHECK(false) << "Trying to lock a process to " << lock_url()
Alex Moshchuk54bf13042017-10-13 04:25:29784 << " but the process is already locked to "
785 << policy->GetOriginLock(process_->GetID());
Alex Moshchuk9cf5a45d2017-08-14 18:50:57786 break;
Alex Moshchuk1656c672018-04-28 01:48:34787 case CheckOriginLockResult::HAS_EQUAL_LOCK:
Alex Moshchuk9cf5a45d2017-08-14 18:50:57788 // Process already has the right origin lock assigned. This case will
789 // happen for commits to |site_| after the first one.
790 break;
791 default:
792 NOTREACHED();
793 }
Alex Moshchuk47dd00f2017-10-10 17:15:23794 } else {
795 // If the site that we've just committed doesn't require a dedicated
796 // process, make sure we aren't putting it in a process for a site that
797 // does.
Alex Moshchuk1656c672018-04-28 01:48:34798 if (lock_state != CheckOriginLockResult::NO_LOCK) {
799 base::debug::SetCrashKeyString(bad_message::GetRequestedSiteURLKey(),
800 site_.spec());
801 base::debug::SetCrashKeyString(
802 bad_message::GetKilledProcessOriginLockKey(),
803 policy->GetOriginLock(process_->GetID()).spec());
804 CHECK(false) << "Trying to commit non-isolated site " << site_
805 << " in process locked to "
806 << policy->GetOriginLock(process_->GetID());
807 }
[email protected]313b80bd2011-11-23 03:49:10808 }
809}
[email protected]46488322012-10-30 03:22:20810
Alex Moshchuk07e1bb42019-03-08 04:44:08811// static
812void SiteInstance::StartIsolatingSite(BrowserContext* context,
813 const GURL& url) {
814 if (!SiteIsolationPolicy::AreDynamicIsolatedOriginsEnabled())
815 return;
816
817 // Ignore attempts to isolate origins that are not supported. Do this here
818 // instead of relying on AddIsolatedOrigins()'s internal validation, to avoid
819 // the runtime warning generated by the latter.
820 url::Origin origin(url::Origin::Create(url));
821 if (!IsolatedOriginUtil::IsValidIsolatedOrigin(origin))
822 return;
823
824 // Convert |url| to a site, to avoid breaking document.domain. Note that
825 // this doesn't use effective URL resolution or other special cases from
826 // GetSiteForURL() and simply converts |origin| to a scheme and eTLD+1.
827 GURL site(SiteInstanceImpl::GetSiteForOrigin(origin));
828
829 ChildProcessSecurityPolicyImpl* policy =
830 ChildProcessSecurityPolicyImpl::GetInstance();
831 policy->AddIsolatedOrigins({url::Origin::Create(site)}, context);
832}
833
[email protected]46488322012-10-30 03:22:20834} // namespace content