blob: 41483a0b41ea929dc503ca0a94d667b289aba7ba [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
[email protected]b6583592012-01-25 19:52:3335SiteInstanceImpl::SiteInstanceImpl(BrowsingInstance* browsing_instance)
[email protected]992db4c2011-05-12 15:37:1536 : id_(next_site_instance_id_++),
creiscce56cd2014-09-29 22:45:2237 active_frame_count_(0),
[email protected]992db4c2011-05-12 15:37:1538 browsing_instance_(browsing_instance),
nickd5bbd0b2016-03-31 19:52:4439 process_(nullptr),
Lukasz Anforowicz3caa8372018-06-05 17:22:0740 can_associate_with_spare_process_(true),
nickd5bbd0b2016-03-31 19:52:4441 has_site_(false),
Tsuyoshi Horoeb576e62017-06-28 06:00:4142 process_reuse_policy_(ProcessReusePolicy::DEFAULT),
43 is_for_service_worker_(false) {
[email protected]4566f132009-03-12 01:55:1344 DCHECK(browsing_instance);
[email protected]4566f132009-03-12 01:55:1345}
46
[email protected]b6583592012-01-25 19:52:3347SiteInstanceImpl::~SiteInstanceImpl() {
[email protected]46488322012-10-30 03:22:2048 GetContentClient()->browser()->SiteInstanceDeleting(this);
[email protected]056ad2a2011-07-12 02:13:5549
Charlie Reiscf6aa512019-04-16 17:27:2950 if (process_) {
[email protected]40bfaf32013-11-19 01:35:4351 process_->RemoveObserver(this);
52
Charlie Reiscf6aa512019-04-16 17:27:2953 // Ensure the RenderProcessHost gets deleted if this SiteInstance created a
54 // process which was never used by any listeners.
55 process_->Cleanup();
56 }
57
initial.commit09911bf2008-07-26 23:55:2958 // Now that no one is referencing us, we can safely remove ourselves from
59 // the BrowsingInstance. Any future visits to a page from this site
60 // (within the same BrowsingInstance) can safely create a new SiteInstance.
61 if (has_site_)
dchengbccd6b82016-03-30 16:24:1962 browsing_instance_->UnregisterSiteInstance(this);
63}
64
Nasko Oskovdeb6c7ea2017-11-30 18:18:1165// static
dchengbccd6b82016-03-30 16:24:1966scoped_refptr<SiteInstanceImpl> SiteInstanceImpl::Create(
67 BrowserContext* browser_context) {
Lukasz Anforowicz4726a172018-10-15 21:25:1068 DCHECK(browser_context);
kylecharda69d882017-10-04 05:49:5269 return base::WrapRefCounted(
dchengbccd6b82016-03-30 16:24:1970 new SiteInstanceImpl(new BrowsingInstance(browser_context)));
71}
72
Nasko Oskovdeb6c7ea2017-11-30 18:18:1173// static
dchengbccd6b82016-03-30 16:24:1974scoped_refptr<SiteInstanceImpl> SiteInstanceImpl::CreateForURL(
75 BrowserContext* browser_context,
76 const GURL& url) {
Lukasz Anforowicz4726a172018-10-15 21:25:1077 DCHECK(browser_context);
dchengbccd6b82016-03-30 16:24:1978 // This will create a new SiteInstance and BrowsingInstance.
79 scoped_refptr<BrowsingInstance> instance(
80 new BrowsingInstance(browser_context));
Aaron Colwellddeccbdb2019-03-08 01:11:0381 return instance->GetSiteInstanceForURL(url,
82 /* allow_default_instance */ false);
initial.commit09911bf2008-07-26 23:55:2983}
84
Nasko Oskovdeb6c7ea2017-11-30 18:18:1185// static
86bool SiteInstanceImpl::ShouldAssignSiteForURL(const GURL& url) {
87 // about:blank should not "use up" a new SiteInstance. The SiteInstance can
88 // still be used for a normal web site.
89 if (url == url::kAboutBlankURL)
90 return false;
91
92 // The embedder will then have the opportunity to determine if the URL
93 // should "use up" the SiteInstance.
94 return GetContentClient()->browser()->ShouldAssignSiteForURL(url);
95}
96
Nicolas Pena7c7847f2018-05-30 01:36:0597// static
98bool SiteInstanceImpl::IsOriginLockASite(const GURL& lock_url) {
99 return lock_url.has_scheme() && lock_url.has_host();
100}
101
avib7348942015-12-25 20:57:10102int32_t SiteInstanceImpl::GetId() {
[email protected]b6583592012-01-25 19:52:33103 return id_;
104}
105
Alex Moshchuk8e5c1952019-01-15 03:39:50106const IsolationContext& SiteInstanceImpl::GetIsolationContext() {
107 return browsing_instance_->isolation_context();
108}
109
Nate Chapin71da03c2019-02-05 01:21:41110RenderProcessHost* SiteInstanceImpl::GetDefaultProcessIfUsable() {
111 if (!base::FeatureList::IsEnabled(
112 features::kProcessSharingWithStrictSiteInstances)) {
113 return nullptr;
114 }
115 if (RequiresDedicatedProcess())
116 return nullptr;
117 return browsing_instance_->default_process();
118}
119
Aaron Colwellddeccbdb2019-03-08 01:11:03120bool SiteInstanceImpl::IsDefaultSiteInstance() {
121 return browsing_instance_->IsDefaultSiteInstance(this);
122}
123
Nate Chapin71da03c2019-02-05 01:21:41124void SiteInstanceImpl::MaybeSetBrowsingInstanceDefaultProcess() {
125 if (!base::FeatureList::IsEnabled(
126 features::kProcessSharingWithStrictSiteInstances)) {
127 return;
128 }
129 // Wait until this SiteInstance both has a site and a process
130 // assigned, so that we can be sure that RequiresDedicatedProcess()
131 // is accurate and we actually have a process to set.
132 if (!process_ || !has_site_ || RequiresDedicatedProcess())
133 return;
134 if (browsing_instance_->default_process()) {
135 DCHECK_EQ(process_, browsing_instance_->default_process());
136 return;
137 }
138 browsing_instance_->SetDefaultProcess(process_);
139}
140
Alex Moshchuk8e5c1952019-01-15 03:39:50141// static
142BrowsingInstanceId SiteInstanceImpl::NextBrowsingInstanceId() {
143 return BrowsingInstance::NextBrowsingInstanceId();
144}
145
146bool SiteInstanceImpl::HasProcess() {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28147 if (process_ != nullptr)
[email protected]41fb79a52012-06-29 16:34:33148 return true;
149
150 // If we would use process-per-site for this site, also check if there is an
151 // existing process that we would use if GetProcess() were called.
Alex Moshchukf7488792019-03-11 22:37:57152 BrowserContext* browser_context = browsing_instance_->GetBrowserContext();
[email protected]41fb79a52012-06-29 16:34:33153 if (has_site_ &&
[email protected]1ae93fb12013-06-14 03:38:56154 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_) &&
Alex Moshchuk8e5c1952019-01-15 03:39:50155 RenderProcessHostImpl::GetSoleProcessHostForSite(
156 browser_context, GetIsolationContext(), site_, lock_url_)) {
[email protected]41fb79a52012-06-29 16:34:33157 return true;
158 }
159
160 return false;
[email protected]5ab79b02010-04-26 16:47:11161}
162
[email protected]41fb79a52012-06-29 16:34:33163RenderProcessHost* SiteInstanceImpl::GetProcess() {
[email protected]08c8ec7e2010-07-11 17:14:48164 // TODO(erikkay) It would be nice to ensure that the renderer type had been
165 // properly set before we get here. The default tab creation case winds up
166 // with no site set at this point, so it will default to TYPE_NORMAL. This
167 // may not be correct, so we'll wind up potentially creating a process that
168 // we then throw away, or worse sharing a process with the wrong process type.
169 // See crbug.com/43448.
170
initial.commit09911bf2008-07-26 23:55:29171 // Create a new process if ours went away or was reused.
[email protected]4566f132009-03-12 01:55:13172 if (!process_) {
Alex Moshchukf7488792019-03-11 22:37:57173 BrowserContext* browser_context = browsing_instance_->GetBrowserContext();
[email protected]41fb79a52012-06-29 16:34:33174
clamy63960c32017-05-10 22:57:07175 // Check if the ProcessReusePolicy should be updated.
176 bool should_use_process_per_site =
creisa9f04ba2017-05-19 22:27:42177 has_site_ &&
178 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_);
clamy63960c32017-05-10 22:57:07179 if (should_use_process_per_site) {
180 process_reuse_policy_ = ProcessReusePolicy::PROCESS_PER_SITE;
181 } else if (process_reuse_policy_ == ProcessReusePolicy::PROCESS_PER_SITE) {
182 process_reuse_policy_ = ProcessReusePolicy::DEFAULT;
[email protected]41fb79a52012-06-29 16:34:33183 }
184
Alex Moshchuk5f926a52018-08-29 20:57:30185 process_ = RenderProcessHostImpl::GetProcessHostForSiteInstance(this);
avi6d686db2017-01-06 02:28:57186
[email protected]41fb79a52012-06-29 16:34:33187 CHECK(process_);
[email protected]40bfaf32013-11-19 01:35:43188 process_->AddObserver(this);
[email protected]41fb79a52012-06-29 16:34:33189
Nate Chapin71da03c2019-02-05 01:21:41190 MaybeSetBrowsingInstanceDefaultProcess();
191
[email protected]41fb79a52012-06-29 16:34:33192 // If we are using process-per-site, we need to register this process
193 // for the current site so that we can find it again. (If no site is set
194 // at this time, we will register it in SetSite().)
clamy63960c32017-05-10 22:57:07195 if (process_reuse_policy_ == ProcessReusePolicy::PROCESS_PER_SITE &&
196 has_site_) {
Alex Moshchuk5f926a52018-08-29 20:57:30197 RenderProcessHostImpl::RegisterSoleProcessHostForSite(browser_context,
198 process_, this);
[email protected]41fb79a52012-06-29 16:34:33199 }
initial.commit09911bf2008-07-26 23:55:29200
naskob8744d22014-08-28 17:07:43201 TRACE_EVENT2("navigation", "SiteInstanceImpl::GetProcess",
202 "site id", id_, "process id", process_->GetID());
[email protected]46488322012-10-30 03:22:20203 GetContentClient()->browser()->SiteInstanceGotProcess(this);
[email protected]6f371442011-11-09 06:45:46204
[email protected]313b80bd2011-11-23 03:49:10205 if (has_site_)
alexmos13fe1962017-06-28 04:25:12206 LockToOriginIfNeeded();
initial.commit09911bf2008-07-26 23:55:29207 }
[email protected]4566f132009-03-12 01:55:13208 DCHECK(process_);
initial.commit09911bf2008-07-26 23:55:29209
[email protected]4566f132009-03-12 01:55:13210 return process_;
initial.commit09911bf2008-07-26 23:55:29211}
212
Lukasz Anforowicz3caa8372018-06-05 17:22:07213bool SiteInstanceImpl::CanAssociateWithSpareProcess() {
214 return can_associate_with_spare_process_;
215}
216
217void SiteInstanceImpl::PreventAssociationWithSpareProcess() {
218 can_associate_with_spare_process_ = false;
219}
220
[email protected]b6583592012-01-25 19:52:33221void SiteInstanceImpl::SetSite(const GURL& url) {
Charlie Reisc2099752019-04-06 01:21:12222 // TODO(creis): Consider calling ShouldAssignSiteForURL internally, rather
223 // than before multiple call sites. See https://crbug.com/949220.
naskob8744d22014-08-28 17:07:43224 TRACE_EVENT2("navigation", "SiteInstanceImpl::SetSite",
225 "site id", id_, "url", url.possibly_invalid_spec());
initial.commit09911bf2008-07-26 23:55:29226 // A SiteInstance's site should not change.
227 // TODO(creis): When following links or script navigations, we can currently
228 // render pages from other sites in this SiteInstance. This will eventually
229 // be fixed, but until then, we should still not set the site of a
230 // SiteInstance more than once.
231 DCHECK(!has_site_);
232
233 // Remember that this SiteInstance has been used to load a URL, even if the
234 // URL is invalid.
235 has_site_ = true;
Alex Moshchukf7488792019-03-11 22:37:57236 BrowserContext* browser_context = browsing_instance_->GetBrowserContext();
Alex Moshchuk25c64bb2017-12-02 02:50:11237 original_url_ = url;
Aaron Colwellddeccbdb2019-03-08 01:11:03238 browsing_instance_->GetSiteAndLockForURL(
239 url, /* allow_default_instance */ false, &site_, &lock_url_);
initial.commit09911bf2008-07-26 23:55:29240
241 // Now that we have a site, register it with the BrowsingInstance. This
242 // ensures that we won't create another SiteInstance for this site within
243 // the same BrowsingInstance, because all same-site pages within a
244 // BrowsingInstance can script each other.
245 browsing_instance_->RegisterSiteInstance(this);
[email protected]313b80bd2011-11-23 03:49:10246
clamy63960c32017-05-10 22:57:07247 // Update the process reuse policy based on the site.
248 bool should_use_process_per_site =
249 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_);
250 if (should_use_process_per_site) {
251 process_reuse_policy_ = ProcessReusePolicy::PROCESS_PER_SITE;
252 }
253
[email protected]41fb79a52012-06-29 16:34:33254 if (process_) {
alexmos13fe1962017-06-28 04:25:12255 LockToOriginIfNeeded();
[email protected]41fb79a52012-06-29 16:34:33256
257 // Ensure the process is registered for this site if necessary.
clamy63960c32017-05-10 22:57:07258 if (should_use_process_per_site) {
Alex Moshchuk5f926a52018-08-29 20:57:30259 RenderProcessHostImpl::RegisterSoleProcessHostForSite(browser_context,
260 process_, this);
[email protected]41fb79a52012-06-29 16:34:33261 }
Nate Chapin71da03c2019-02-05 01:21:41262 MaybeSetBrowsingInstanceDefaultProcess();
[email protected]41fb79a52012-06-29 16:34:33263 }
initial.commit09911bf2008-07-26 23:55:29264}
265
[email protected]77ab17312012-09-28 15:34:59266const GURL& SiteInstanceImpl::GetSiteURL() const {
[email protected]b6583592012-01-25 19:52:33267 return site_;
268}
269
270bool SiteInstanceImpl::HasSite() const {
271 return has_site_;
272}
273
274bool SiteInstanceImpl::HasRelatedSiteInstance(const GURL& url) {
initial.commit09911bf2008-07-26 23:55:29275 return browsing_instance_->HasSiteInstance(url);
276}
277
dchengbccd6b82016-03-30 16:24:19278scoped_refptr<SiteInstance> SiteInstanceImpl::GetRelatedSiteInstance(
279 const GURL& url) {
Aaron Colwellddeccbdb2019-03-08 01:11:03280 return browsing_instance_->GetSiteInstanceForURL(
281 url, /* allow_default_instance */ true);
initial.commit09911bf2008-07-26 23:55:29282}
283
[email protected]14392a52012-05-02 20:28:44284bool SiteInstanceImpl::IsRelatedSiteInstance(const SiteInstance* instance) {
[email protected]fc72bb12013-06-02 21:13:46285 return browsing_instance_.get() == static_cast<const SiteInstanceImpl*>(
286 instance)->browsing_instance_.get();
[email protected]14392a52012-05-02 20:28:44287}
288
[email protected]d5072a82014-05-15 05:50:18289size_t SiteInstanceImpl::GetRelatedActiveContentsCount() {
290 return browsing_instance_->active_contents_count();
291}
292
[email protected]f88628d02012-11-11 17:58:59293bool SiteInstanceImpl::HasWrongProcessForURL(const GURL& url) {
[email protected]144a8102012-01-14 01:05:31294 // If the URL to navigate to can be associated with any site instance,
295 // we want to keep it in the same process.
[email protected]c02f1ba2014-02-03 06:53:53296 if (IsRendererDebugURL(url))
[email protected]144a8102012-01-14 01:05:31297 return false;
298
Nasko Oskov978c16b2018-08-03 22:17:06299 // Any process can host an about:blank URL, except the one used for error
300 // pages, which should not commit successful navigations. This check avoids a
301 // process transfer for browser-initiated navigations to about:blank in a
302 // dedicated process; without it, IsSuitableHost would consider this process
303 // unsuitable for about:blank when it compares origin locks.
304 // Renderer-initiated navigations will handle about:blank navigations
305 // elsewhere and leave them in the source SiteInstance, along with
306 // about:srcdoc and data:.
307 if (url.IsAboutBlank() && site_ != GURL(kUnreachableWebDataURL))
alexmos13fe1962017-06-28 04:25:12308 return false;
309
[email protected]88aae972011-12-16 01:14:18310 // If the site URL is an extension (e.g., for hosted apps or WebUI) but the
[email protected]d292d8a2011-05-25 03:47:11311 // process is not (or vice versa), make sure we notice and fix it.
Aaron Colwellddeccbdb2019-03-08 01:11:03312 GURL site_url;
313 GURL origin_lock;
314
315 // Note: This call must return information that is identical to what
316 // would be reported in the SiteInstance returned by
317 // GetRelatedSiteInstance(url).
318 browsing_instance_->GetSiteAndLockForURL(
319 url, /* allow_default_instance */ true, &site_url, &origin_lock);
Charlie Reisc2099752019-04-06 01:21:12320
321 // Note that HasProcess() may return true if process_ is null, in
322 // process-per-site cases where there's an existing process available.
323 // We want to use such a process in the IsSuitableHost check, so we
324 // may end up assigning process_ in the GetProcess() call below.
325 if (!HasProcess()) {
326 // If there is no process or site, then this is a new SiteInstance that can
327 // be used for anything.
328 if (!HasSite())
329 return false;
330
331 // If there is no process but there is a site, then the process must have
332 // been discarded after we navigated away. If the site URLs match, then it
333 // is safe to use this SiteInstance.
334 if (GetSiteURL() == site_url)
335 return false;
336
337 // If the site URLs do not match, but neither this SiteInstance nor the
338 // destination site_url require dedicated processes, then it is safe to use
339 // this SiteInstance.
340 if (!RequiresDedicatedProcess() &&
341 !DoesSiteRequireDedicatedProcess(GetIsolationContext(), site_url)) {
342 return false;
343 }
344
345 // Otherwise, there's no process, the site URLs don't match, and at least
346 // one of them requires a dedicated process, so it is not safe to use this
347 // SiteInstance.
348 return true;
349 }
350
[email protected]88aae972011-12-16 01:14:18351 return !RenderProcessHostImpl::IsSuitableHost(
Alex Moshchukf7488792019-03-11 22:37:57352 GetProcess(), browsing_instance_->GetBrowserContext(),
Alex Moshchuk8e5c1952019-01-15 03:39:50353 GetIsolationContext(), site_url, origin_lock);
[email protected]d292d8a2011-05-25 03:47:11354}
355
nickd30fd962015-07-27 21:51:08356bool SiteInstanceImpl::RequiresDedicatedProcess() {
357 if (!has_site_)
358 return false;
nickd5bbd0b2016-03-31 19:52:44359
Lukasz Anforowicz1a0a89a2019-03-29 22:31:02360 return DoesSiteRequireDedicatedProcess(GetIsolationContext(), site_);
nickd30fd962015-07-27 21:51:08361}
362
nick9f34e2892016-01-12 21:01:07363void SiteInstanceImpl::IncrementActiveFrameCount() {
364 active_frame_count_++;
365}
366
367void SiteInstanceImpl::DecrementActiveFrameCount() {
ericwilligers254597b2016-10-17 10:32:31368 if (--active_frame_count_ == 0) {
369 for (auto& observer : observers_)
370 observer.ActiveFrameCountIsZero(this);
371 }
nick9f34e2892016-01-12 21:01:07372}
373
[email protected]d5072a82014-05-15 05:50:18374void SiteInstanceImpl::IncrementRelatedActiveContentsCount() {
375 browsing_instance_->increment_active_contents_count();
376}
377
378void SiteInstanceImpl::DecrementRelatedActiveContentsCount() {
379 browsing_instance_->decrement_active_contents_count();
380}
381
nick9f34e2892016-01-12 21:01:07382void SiteInstanceImpl::AddObserver(Observer* observer) {
383 observers_.AddObserver(observer);
384}
385
386void SiteInstanceImpl::RemoveObserver(Observer* observer) {
387 observers_.RemoveObserver(observer);
388}
389
[email protected]4c3a23582012-08-18 08:54:34390BrowserContext* SiteInstanceImpl::GetBrowserContext() const {
Alex Moshchukf7488792019-03-11 22:37:57391 return browsing_instance_->GetBrowserContext();
[email protected]72daaa92012-01-18 13:39:02392}
393
nickcc0d9142015-10-14 16:27:10394// static
dchengbccd6b82016-03-30 16:24:19395scoped_refptr<SiteInstance> SiteInstance::Create(
396 BrowserContext* browser_context) {
Lukasz Anforowicz4726a172018-10-15 21:25:10397 DCHECK(browser_context);
dchengbccd6b82016-03-30 16:24:19398 return SiteInstanceImpl::Create(browser_context);
initial.commit09911bf2008-07-26 23:55:29399}
400
nickcc0d9142015-10-14 16:27:10401// static
dchengbccd6b82016-03-30 16:24:19402scoped_refptr<SiteInstance> SiteInstance::CreateForURL(
403 BrowserContext* browser_context,
404 const GURL& url) {
Lukasz Anforowicz4726a172018-10-15 21:25:10405 DCHECK(browser_context);
dchengbccd6b82016-03-30 16:24:19406 return SiteInstanceImpl::CreateForURL(browser_context, url);
[email protected]d8a96622009-05-07 19:21:16407}
408
nickcc0d9142015-10-14 16:27:10409// static
Nasko Oskovdeb6c7ea2017-11-30 18:18:11410bool SiteInstance::ShouldAssignSiteForURL(const GURL& url) {
411 return SiteInstanceImpl::ShouldAssignSiteForURL(url);
412}
413
Alex Moshchuk78cf66bda2018-11-30 01:49:30414bool SiteInstanceImpl::IsSameSiteWithURL(const GURL& url) {
415 return SiteInstanceImpl::IsSameWebSite(
Lukasz Anforowicz1a0a89a2019-03-29 22:31:02416 GetIsolationContext(), site_, url,
417 true /* should_compare_effective_urls */);
Alex Moshchuk25c64bb2017-12-02 02:50:11418}
419
Alex Moshchuk78cf66bda2018-11-30 01:49:30420// static
Lukasz Anforowicz1a0a89a2019-03-29 22:31:02421bool SiteInstanceImpl::IsSameWebSite(const IsolationContext& isolation_context,
Alex Moshchuk25c64bb2017-12-02 02:50:11422 const GURL& real_src_url,
423 const GURL& real_dest_url,
424 bool should_compare_effective_urls) {
Lukasz Anforowicz1a0a89a2019-03-29 22:31:02425 DCHECK_CURRENTLY_ON(BrowserThread::UI);
426 BrowserContext* browser_context =
427 isolation_context.browser_or_resource_context().ToBrowserContext();
Lukasz Anforowicz4726a172018-10-15 21:25:10428 DCHECK(browser_context);
429
Alex Moshchuk25c64bb2017-12-02 02:50:11430 GURL src_url =
431 should_compare_effective_urls
432 ? SiteInstanceImpl::GetEffectiveURL(browser_context, real_src_url)
433 : real_src_url;
434 GURL dest_url =
435 should_compare_effective_urls
436 ? SiteInstanceImpl::GetEffectiveURL(browser_context, real_dest_url)
437 : real_dest_url;
[email protected]399583b2012-12-11 09:33:42438
439 // We infer web site boundaries based on the registered domain name of the
440 // top-level page and the scheme. We do not pay attention to the port if
441 // one is present, because pages served from different ports can still
442 // access each other if they change their document.domain variable.
443
444 // Some special URLs will match the site instance of any other URL. This is
445 // done before checking both of them for validity, since we want these URLs
446 // to have the same site instance as even an invalid one.
[email protected]855d7d572014-08-02 11:18:17447 if (IsRendererDebugURL(src_url) || IsRendererDebugURL(dest_url))
[email protected]399583b2012-12-11 09:33:42448 return true;
449
450 // If either URL is invalid, they aren't part of the same site.
[email protected]855d7d572014-08-02 11:18:17451 if (!src_url.is_valid() || !dest_url.is_valid())
[email protected]399583b2012-12-11 09:33:42452 return false;
453
[email protected]855d7d572014-08-02 11:18:17454 // If the destination url is just a blank page, we treat them as part of the
455 // same site.
456 GURL blank_page(url::kAboutBlankURL);
457 if (dest_url == blank_page)
458 return true;
459
Alex Moshchuka308c9b2018-02-08 20:58:14460 // If the source and destination URLs are equal excluding the hash, they have
461 // the same site. This matters for file URLs, where SameDomainOrHost() would
462 // otherwise return false below.
463 if (src_url.EqualsIgnoringRef(dest_url))
464 return true;
465
Daniel Cheng88186bd52017-10-20 08:14:46466 url::Origin src_origin = url::Origin::Create(src_url);
467 url::Origin dest_origin = url::Origin::Create(dest_url);
alexmos3b9ad102017-05-26 23:41:08468
[email protected]399583b2012-12-11 09:33:42469 // If the schemes differ, they aren't part of the same site.
alexmoscbf995782017-06-01 03:13:13470 if (src_origin.scheme() != dest_origin.scheme())
[email protected]399583b2012-12-11 09:33:42471 return false;
472
alexmos4bc26322017-07-01 00:57:14473 if (!net::registry_controlled_domains::SameDomainOrHost(
474 src_origin, dest_origin,
475 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) {
476 return false;
477 }
478
479 // If the sites are the same, check isolated origins. If either URL matches
Alex Moshchuk4e19b362018-09-10 21:14:36480 // an isolated origin, compare origins rather than sites. As an optimization
481 // to avoid unneeded isolated origin lookups, shortcut this check if the two
482 // origins are the same.
483 if (src_origin == dest_origin)
484 return true;
alexmos4bc26322017-07-01 00:57:14485 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
486 url::Origin src_isolated_origin;
487 url::Origin dest_isolated_origin;
Alex Moshchuk8e5c1952019-01-15 03:39:50488 bool src_origin_is_isolated = policy->GetMatchingIsolatedOrigin(
489 isolation_context, src_origin, &src_isolated_origin);
490 bool dest_origin_is_isolated = policy->GetMatchingIsolatedOrigin(
491 isolation_context, dest_origin, &dest_isolated_origin);
alexmos4bc26322017-07-01 00:57:14492 if (src_origin_is_isolated || dest_origin_is_isolated) {
493 // Compare most specific matching origins to ensure that a subdomain of an
494 // isolated origin (e.g., https://subdomain.isolated.foo.com) also matches
495 // the isolated origin's site URL (e.g., https://isolated.foo.com).
496 return src_isolated_origin == dest_isolated_origin;
497 }
498
499 return true;
[email protected]399583b2012-12-11 09:33:42500}
501
nickcc0d9142015-10-14 16:27:10502// static
[email protected]399583b2012-12-11 09:33:42503GURL SiteInstance::GetSiteForURL(BrowserContext* browser_context,
Alex Moshchuk5f926a52018-08-29 20:57:30504 const GURL& url) {
Aaron Colwellea6921f2019-01-29 16:50:39505 DCHECK_CURRENTLY_ON(BrowserThread::UI);
506 DCHECK(browser_context);
507
Alex Moshchuk5f926a52018-08-29 20:57:30508 // By default, GetSiteForURL will resolve |real_url| to an effective URL
Alex Moshchuk8e5c1952019-01-15 03:39:50509 // before computing its site, so set |should_use_effective_urls| to true.
510 //
511 // TODO(alexmos): Callers inside content/ should already be using the
512 // internal SiteInstanceImpl version and providing a proper IsolationContext.
513 // For callers outside content/, plumb the applicable IsolationContext here,
514 // where needed. Eventually, GetSiteForURL should always require an
515 // IsolationContext to be passed in, and this implementation should just
516 // become SiteInstanceImpl::GetSiteForURL.
Aaron Colwellea6921f2019-01-29 16:50:39517 return SiteInstanceImpl::GetSiteForURL(
Alex Moshchuk99b795422019-03-07 00:27:32518 IsolationContext(browser_context), url,
Aaron Colwellea6921f2019-01-29 16:50:39519 true /* should_use_effective_urls */);
Alex Moshchuk5f926a52018-08-29 20:57:30520}
521
522// static
Alex Moshchuk8e5c1952019-01-15 03:39:50523GURL SiteInstanceImpl::DetermineProcessLockURL(
Alex Moshchuk8e5c1952019-01-15 03:39:50524 const IsolationContext& isolation_context,
525 const GURL& url) {
Alex Moshchuk5f926a52018-08-29 20:57:30526 // For the process lock URL, convert |url| to a site without resolving |url|
527 // to an effective URL.
Alex Moshchukf7488792019-03-11 22:37:57528 return SiteInstanceImpl::GetSiteForURL(isolation_context, url,
Alex Moshchuk5f926a52018-08-29 20:57:30529 false /* should_use_effective_urls */);
530}
531
Aaron Colwellea6921f2019-01-29 16:50:39532// static
Alex Moshchukf7488792019-03-11 22:37:57533GURL SiteInstanceImpl::GetSiteForURL(const IsolationContext& isolation_context,
Alex Moshchuk5f926a52018-08-29 20:57:30534 const GURL& real_url,
535 bool should_use_effective_urls) {
[email protected]25a9e8e532012-06-22 06:16:28536 // TODO(fsamuel, creis): For some reason appID is not recognized as a host.
[email protected]6eb1a11e2013-10-09 00:54:37537 if (real_url.SchemeIs(kGuestScheme))
[email protected]25a9e8e532012-06-22 06:16:28538 return real_url;
539
Aaron Colwellea6921f2019-01-29 16:50:39540 if (should_use_effective_urls)
541 DCHECK_CURRENTLY_ON(BrowserThread::UI);
542
Alex Moshchuk5f926a52018-08-29 20:57:30543 GURL url = should_use_effective_urls
Alex Moshchukf7488792019-03-11 22:37:57544 ? SiteInstanceImpl::GetEffectiveURL(
545 isolation_context.browser_or_resource_context()
546 .ToBrowserContext(),
547 real_url)
Alex Moshchuk5f926a52018-08-29 20:57:30548 : real_url;
Daniel Cheng88186bd52017-10-20 08:14:46549 url::Origin origin = url::Origin::Create(url);
[email protected]3a8eecb2010-04-22 23:56:30550
Lukasz Anforowicz48097c42017-12-15 00:23:38551 // If the url has a host, then determine the site. Skip file URLs to avoid a
552 // situation where site URL of file://localhost/ would mismatch Blink's origin
553 // (which ignores the hostname in this case - see https://crbug.com/776160).
554 if (!origin.host().empty() && origin.scheme() != url::kFileScheme) {
Alex Moshchuk4e19b362018-09-10 21:14:36555 GURL site_url(GetSiteForOrigin(origin));
556
557 // Isolated origins should use the full origin as their site URL. A
558 // subdomain of an isolated origin should also use that isolated origin's
559 // site URL. It is important to check |origin| (based on |url|) rather than
560 // |real_url| here, since some effective URLs (such as for NTP) need to be
561 // resolved prior to the isolated origin lookup.
562 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
563 url::Origin isolated_origin;
Alex Moshchuk8e5c1952019-01-15 03:39:50564 if (policy->GetMatchingIsolatedOrigin(isolation_context, origin, site_url,
565 &isolated_origin))
Alex Moshchuk4e19b362018-09-10 21:14:36566 return isolated_origin.GetURL();
Alex Moshchuk5f926a52018-08-29 20:57:30567
568 // If an effective URL was used, augment the effective site URL with the
569 // underlying web site in the hash. This is needed to keep
570 // navigations across sites covered by one hosted app in separate
571 // SiteInstances. See https://crbug.com/791796.
572 //
573 // TODO(https://crbug.com/734722): Consider replacing this hack with
574 // a proper security principal.
575 if (should_use_effective_urls && url != real_url) {
Alex Moshchuk4e19b362018-09-10 21:14:36576 std::string non_translated_site_url(
Alex Moshchukf7488792019-03-11 22:37:57577 GetSiteForURL(isolation_context, real_url,
Alex Moshchuk4e19b362018-09-10 21:14:36578 false /* should_use_effective_urls */)
579 .spec());
580 GURL::Replacements replacements;
581 replacements.SetRefStr(non_translated_site_url.c_str());
582 site_url = site_url.ReplaceComponents(replacements);
Alex Moshchuk5f926a52018-08-29 20:57:30583 }
584
Alex Moshchuk4e19b362018-09-10 21:14:36585 return site_url;
initial.commit09911bf2008-07-26 23:55:29586 }
creisf60c2cd2014-12-18 00:41:02587
588 // If there is no host but there is a scheme, return the scheme.
589 // This is useful for cases like file URLs.
Chris Palmerab5e5b52018-09-28 19:19:30590 if (!origin.opaque()) {
Lukasz Anforowicz217fd272018-03-07 21:41:43591 // Prefer to use the scheme of |origin| rather than |url|, to correctly
Alex Moshchukb1f87482018-07-19 01:51:51592 // cover blob:file: and filesystem:file: URIs (see also
593 // https://crbug.com/697111).
Lukasz Anforowiczd926a842018-03-09 01:50:45594 DCHECK(!origin.scheme().empty());
595 return GURL(origin.scheme() + ":");
596 } else if (url.has_scheme()) {
Alex Moshchukb1f87482018-07-19 01:51:51597 // In some cases, it is not safe to use just the scheme as a site URL, as
Charlie Reis0bb3f5c72018-08-06 22:46:01598 // that might allow two URLs created by different sites to share a process.
599 // See https://crbug.com/863623 and https://crbug.com/863069.
Alex Moshchukb1f87482018-07-19 01:51:51600 //
601 // TODO(alexmos,creis): This should eventually be expanded to certain other
Charlie Reis0bb3f5c72018-08-06 22:46:01602 // schemes, such as file:.
603 // TODO(creis): This currently causes problems with tests on Android and
604 // Android WebView. For now, skip it when Site Isolation is not enabled,
605 // since there's no need to isolate data and blob URLs from each other in
606 // that case.
607 bool is_site_isolation_enabled =
608 SiteIsolationPolicy::UseDedicatedProcessesForAllSites() ||
609 SiteIsolationPolicy::AreIsolatedOriginsEnabled();
610 if (is_site_isolation_enabled &&
611 (url.SchemeIsBlob() || url.scheme() == url::kDataScheme)) {
Alex Moshchukb1f87482018-07-19 01:51:51612 // We get here for blob URLs of form blob:null/guid. Use the full URL
613 // with the guid in that case, which isolates all blob URLs with unique
Charlie Reis0bb3f5c72018-08-06 22:46:01614 // origins from each other. We also get here for browser-initiated
615 // navigations to data URLs, which have a unique origin and should only
616 // share a process when they are identical. Remove hash from the URL in
617 // either case, since same-document navigations shouldn't use a different
618 // site URL.
Alex Moshchukb1f87482018-07-19 01:51:51619 if (url.has_ref()) {
620 GURL::Replacements replacements;
621 replacements.ClearRef();
622 url = url.ReplaceComponents(replacements);
623 }
624 return url;
625 }
626
Lukasz Anforowiczd926a842018-03-09 01:50:45627 DCHECK(!url.scheme().empty());
creisf60c2cd2014-12-18 00:41:02628 return GURL(url.scheme() + ":");
Lukasz Anforowiczd926a842018-03-09 01:50:45629 }
creisf60c2cd2014-12-18 00:41:02630
631 // Otherwise the URL should be invalid; return an empty site.
Lukasz Anforowicz217fd272018-03-07 21:41:43632 DCHECK(!url.is_valid()) << url;
creisf60c2cd2014-12-18 00:41:02633 return GURL();
initial.commit09911bf2008-07-26 23:55:29634}
635
nickcc0d9142015-10-14 16:27:10636// static
Alex Moshchuk4e19b362018-09-10 21:14:36637GURL SiteInstanceImpl::GetSiteForOrigin(const url::Origin& origin) {
638 // Only keep the scheme and registered domain of |origin|.
639 std::string domain = net::registry_controlled_domains::GetDomainAndRegistry(
640 origin.host(),
641 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
642 std::string site = origin.scheme();
643 site += url::kStandardSchemeSeparator;
644 site += domain.empty() ? origin.host() : domain;
645 return GURL(site);
646}
647
648// static
[email protected]4c3a23582012-08-18 08:54:34649GURL SiteInstanceImpl::GetEffectiveURL(BrowserContext* browser_context,
650 const GURL& url) {
Aaron Colwellea6921f2019-01-29 16:50:39651 DCHECK(browser_context);
Alex Moshchuka31c7882018-01-17 00:57:30652 return GetContentClient()->browser()->GetEffectiveURL(browser_context, url);
[email protected]3a8eecb2010-04-22 23:56:30653}
654
nickcc0d9142015-10-14 16:27:10655// static
Alex Moshchuk25c64bb2017-12-02 02:50:11656bool SiteInstanceImpl::HasEffectiveURL(BrowserContext* browser_context,
657 const GURL& url) {
658 return GetEffectiveURL(browser_context, url) != url;
659}
660
661// static
nickcc0d9142015-10-14 16:27:10662bool SiteInstanceImpl::DoesSiteRequireDedicatedProcess(
Alex Moshchuk8e5c1952019-01-15 03:39:50663 const IsolationContext& isolation_context,
nickdb193a12016-09-09 23:09:23664 const GURL& url) {
Lukasz Anforowicz4aff3b82019-04-04 16:00:33665 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO) ||
666 BrowserThread::CurrentlyOn(BrowserThread::UI));
667 DCHECK(isolation_context.browser_or_resource_context());
Lukasz Anforowicz4726a172018-10-15 21:25:10668
nickcc0d9142015-10-14 16:27:10669 // If --site-per-process is enabled, site isolation is enabled everywhere.
670 if (SiteIsolationPolicy::UseDedicatedProcessesForAllSites())
671 return true;
672
alexmos3b9ad102017-05-26 23:41:08673 // Always require a dedicated process for isolated origins.
Alex Moshchukf7488792019-03-11 22:37:57674 GURL site_url = SiteInstanceImpl::GetSiteForURL(
675 isolation_context, url, true /* should_compare_effective_urls */);
alexmos3b9ad102017-05-26 23:41:08676 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
Alex Moshchuk8e5c1952019-01-15 03:39:50677 if (policy->IsIsolatedOrigin(isolation_context,
678 url::Origin::Create(site_url)))
alexmos3b9ad102017-05-26 23:41:08679 return true;
680
Lukasz Anforowicz1f5ad402018-09-28 23:57:54681 // Error pages in main frames do require isolation, however since this is
682 // missing the context whether this is for a main frame or not, that part
683 // is enforced in RenderFrameHostManager.
684 if (site_url.SchemeIs(kChromeErrorScheme))
685 return true;
686
687 // Isolate kChromeUIScheme pages from one another and from other kinds of
688 // schemes.
689 if (site_url.SchemeIs(content::kChromeUIScheme))
690 return true;
691
nickdb193a12016-09-09 23:09:23692 // Let the content embedder enable site isolation for specific URLs. Use the
693 // canonical site url for this check, so that schemes with nested origins
694 // (blob and filesystem) work properly.
Avi Drissman8c36df72017-12-11 21:19:20695 if (GetContentClient()->browser()->DoesSiteRequireDedicatedProcess(
Lukasz Anforowicz4aff3b82019-04-04 16:00:33696 isolation_context.browser_or_resource_context(), site_url)) {
nickcc0d9142015-10-14 16:27:10697 return true;
698 }
699
700 return false;
701}
702
alexmos13fe1962017-06-28 04:25:12703// static
Alex Moshchuk8e5c1952019-01-15 03:39:50704bool SiteInstanceImpl::ShouldLockToOrigin(
Alex Moshchuk8e5c1952019-01-15 03:39:50705 const IsolationContext& isolation_context,
706 GURL site_url) {
Lukasz Anforowicz1a0a89a2019-03-29 22:31:02707 DCHECK_CURRENTLY_ON(BrowserThread::UI);
708 BrowserContext* browser_context =
709 isolation_context.browser_or_resource_context().ToBrowserContext();
Lukasz Anforowicz4726a172018-10-15 21:25:10710 DCHECK(browser_context);
711
creis0b49fa12017-07-10 16:31:11712 // Don't lock to origin in --single-process mode, since this mode puts
713 // cross-site pages into the same process.
Alex Moshchukd4c4ca32018-08-15 20:36:51714 if (RenderProcessHost::run_renderer_in_process())
creis0b49fa12017-07-10 16:31:11715 return false;
716
Lukasz Anforowicz1a0a89a2019-03-29 22:31:02717 if (!DoesSiteRequireDedicatedProcess(isolation_context, site_url))
alexmos13fe1962017-06-28 04:25:12718 return false;
719
720 // Guest processes cannot be locked to their site because guests always have
721 // a fixed SiteInstance. The site of GURLs a guest loads doesn't match that
722 // SiteInstance. So we skip locking the guest process to the site.
723 // TODO(ncarter): Remove this exclusion once we can make origin lock per
724 // RenderFrame routing id.
725 if (site_url.SchemeIs(content::kGuestScheme))
726 return false;
727
alexmos13fe1962017-06-28 04:25:12728 // TODO(creis, nick): Until we can handle sites with effective URLs at the
729 // call sites of ChildProcessSecurityPolicy::CanAccessDataForOrigin, we
730 // must give the embedder a chance to exempt some sites to avoid process
731 // kills.
732 if (!GetContentClient()->browser()->ShouldLockToOrigin(browser_context,
733 site_url)) {
734 return false;
735 }
736
737 return true;
738}
739
Lukasz Anforowicz27851e62018-12-18 23:04:08740// static
741base::Optional<url::Origin> SiteInstanceImpl::GetRequestInitiatorSiteLock(
Lukasz Anforowicz27851e62018-12-18 23:04:08742 GURL site_url) {
Lukasz Anforowicz27851e62018-12-18 23:04:08743 // The following schemes are safe for sites that require a process lock:
744 // - data: - locking |request_initiator| to an opaque origin
745 // - http/https - requiring |request_initiator| to match |site_url| with
746 // DomainIs (i.e. suffix-based) comparison.
747 if (site_url.SchemeIsHTTPOrHTTPS() || site_url.SchemeIs(url::kDataScheme))
748 return url::Origin::Create(site_url);
749
750 // Other schemes might not be safe to use as |request_initiator_site_lock|.
751 // One example is chrome-guest://...
752 return base::nullopt;
753}
754
[email protected]40bfaf32013-11-19 01:35:43755void SiteInstanceImpl::RenderProcessHostDestroyed(RenderProcessHost* host) {
756 DCHECK_EQ(process_, host);
757 process_->RemoveObserver(this);
nickd5bbd0b2016-03-31 19:52:44758 process_ = nullptr;
[email protected]4566f132009-03-12 01:55:13759}
[email protected]313b80bd2011-11-23 03:49:10760
Bo Liu2a489402018-04-24 23:41:27761void SiteInstanceImpl::RenderProcessExited(
762 RenderProcessHost* host,
763 const ChildProcessTerminationInfo& info) {
ericwilligers254597b2016-10-17 10:32:31764 for (auto& observer : observers_)
765 observer.RenderProcessGone(this);
nick9f34e2892016-01-12 21:01:07766}
767
alexmos13fe1962017-06-28 04:25:12768void SiteInstanceImpl::LockToOriginIfNeeded() {
769 DCHECK(HasSite());
770
771 // From now on, this process should be considered "tainted" for future
772 // process reuse decisions:
773 // (1) If |site_| required a dedicated process, this SiteInstance's process
774 // can only host URLs for the same site.
775 // (2) Even if |site_| does not require a dedicated process, this
776 // SiteInstance's process still cannot be reused to host other sites
777 // requiring dedicated sites in the future.
778 // We can get here either when we commit a URL into a SiteInstance that does
779 // not yet have a site, or when we create a process for a SiteInstance with a
780 // preassigned site.
781 process_->SetIsUsed();
782
Alex Moshchuk47dd00f2017-10-10 17:15:23783 ChildProcessSecurityPolicyImpl* policy =
784 ChildProcessSecurityPolicyImpl::GetInstance();
Alex Moshchuk58225c82019-04-18 00:45:01785 GURL process_lock = policy->GetOriginLock(process_->GetID());
Lukasz Anforowicz1a0a89a2019-03-29 22:31:02786 if (ShouldLockToOrigin(GetIsolationContext(), site_)) {
Alex Moshchuk9cf5a45d2017-08-14 18:50:57787 // Sanity check that this won't try to assign an origin lock to a <webview>
788 // process, which can't be locked.
789 CHECK(!process_->IsForGuestsOnly());
790
Alex Moshchuk58225c82019-04-18 00:45:01791 if (process_lock.is_empty()) {
792 // TODO(nick): When all sites are isolated, this operation provides
793 // strong protection. If only some sites are isolated, we need
794 // additional logic to prevent the non-isolated sites from requesting
795 // resources for isolated sites. https://crbug.com/509125
796 TRACE_EVENT2("navigation", "SiteInstanceImpl::LockToOrigin", "site id",
797 id_, "lock", lock_url().possibly_invalid_spec());
798 process_->LockToOrigin(GetIsolationContext(), lock_url());
799 } else if (process_lock != lock_url()) {
800 // We should never attempt to reassign a different origin lock to a
801 // process.
802 base::debug::SetCrashKeyString(bad_message::GetRequestedSiteURLKey(),
803 site_.spec());
804 base::debug::SetCrashKeyString(
805 bad_message::GetKilledProcessOriginLockKey(), process_lock.spec());
806 CHECK(false) << "Trying to lock a process to " << lock_url()
807 << " but the process is already locked to " << process_lock;
808 } else {
809 // Process already has the right origin lock assigned. This case will
810 // happen for commits to |site_| after the first one.
Alex Moshchuk9cf5a45d2017-08-14 18:50:57811 }
Alex Moshchuk47dd00f2017-10-10 17:15:23812 } else {
813 // If the site that we've just committed doesn't require a dedicated
814 // process, make sure we aren't putting it in a process for a site that
815 // does.
Alex Moshchuk58225c82019-04-18 00:45:01816 if (!process_lock.is_empty()) {
Alex Moshchuk1656c672018-04-28 01:48:34817 base::debug::SetCrashKeyString(bad_message::GetRequestedSiteURLKey(),
818 site_.spec());
819 base::debug::SetCrashKeyString(
Alex Moshchuk58225c82019-04-18 00:45:01820 bad_message::GetKilledProcessOriginLockKey(), process_lock.spec());
Alex Moshchuk1656c672018-04-28 01:48:34821 CHECK(false) << "Trying to commit non-isolated site " << site_
Alex Moshchuk58225c82019-04-18 00:45:01822 << " in process locked to " << process_lock;
Alex Moshchuk1656c672018-04-28 01:48:34823 }
[email protected]313b80bd2011-11-23 03:49:10824 }
825}
[email protected]46488322012-10-30 03:22:20826
Alex Moshchuk07e1bb42019-03-08 04:44:08827// static
828void SiteInstance::StartIsolatingSite(BrowserContext* context,
829 const GURL& url) {
830 if (!SiteIsolationPolicy::AreDynamicIsolatedOriginsEnabled())
831 return;
832
833 // Ignore attempts to isolate origins that are not supported. Do this here
834 // instead of relying on AddIsolatedOrigins()'s internal validation, to avoid
835 // the runtime warning generated by the latter.
836 url::Origin origin(url::Origin::Create(url));
837 if (!IsolatedOriginUtil::IsValidIsolatedOrigin(origin))
838 return;
839
840 // Convert |url| to a site, to avoid breaking document.domain. Note that
841 // this doesn't use effective URL resolution or other special cases from
842 // GetSiteForURL() and simply converts |origin| to a scheme and eTLD+1.
843 GURL site(SiteInstanceImpl::GetSiteForOrigin(origin));
844
845 ChildProcessSecurityPolicyImpl* policy =
846 ChildProcessSecurityPolicyImpl::GetInstance();
847 policy->AddIsolatedOrigins({url::Origin::Create(site)}, context);
848}
849
[email protected]46488322012-10-30 03:22:20850} // namespace content