[email protected] | 144a810 | 2012-01-14 01:05:31 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 5 | #include "content/browser/site_instance_impl.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
Lukasz Anforowicz | 48097c4 | 2017-12-15 00:23:38 | [diff] [blame] | 7 | #include <string> |
| 8 | |
creis | 0b49fa1 | 2017-07-10 16:31:11 | [diff] [blame] | 9 | #include "base/command_line.h" |
Alex Moshchuk | 4479b97c | 2017-10-17 23:20:32 | [diff] [blame] | 10 | #include "base/debug/crash_logging.h" |
Aaron Colwell | 6fee3f5 | 2019-05-08 21:35:02 | [diff] [blame] | 11 | #include "base/lazy_instance.h" |
alexmos | 3b9ad10 | 2017-05-26 23:41:08 | [diff] [blame] | 12 | #include "base/macros.h" |
[email protected] | 3936521 | 2011-02-24 01:01:00 | [diff] [blame] | 13 | #include "content/browser/browsing_instance.h" |
[email protected] | b953542 | 2012-02-09 01:47:59 | [diff] [blame] | 14 | #include "content/browser/child_process_security_policy_impl.h" |
[email protected] | c02f1ba | 2014-02-03 06:53:53 | [diff] [blame] | 15 | #include "content/browser/frame_host/debug_urls.h" |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 16 | #include "content/browser/frame_host/frame_tree_node.h" |
Alex Moshchuk | 07e1bb4 | 2019-03-08 04:44:08 | [diff] [blame] | 17 | #include "content/browser/isolated_origin_util.h" |
Alex Moshchuk | 8e5c195 | 2019-01-15 03:39:50 | [diff] [blame] | 18 | #include "content/browser/isolation_context.h" |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 19 | #include "content/browser/renderer_host/render_process_host_impl.h" |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 20 | #include "content/browser/storage_partition_impl.h" |
Aaron Colwell | ea6921f | 2019-01-29 16:50:39 | [diff] [blame] | 21 | #include "content/public/browser/browser_or_resource_context.h" |
[email protected] | 87f3c08 | 2011-10-19 18:07:44 | [diff] [blame] | 22 | #include "content/public/browser/content_browser_client.h" |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 23 | #include "content/public/browser/render_process_host_factory.h" |
Nick Carter | bf6264a5 | 2018-04-06 02:39:33 | [diff] [blame] | 24 | #include "content/public/browser/site_isolation_policy.h" |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 25 | #include "content/public/browser/web_ui_controller_factory.h" |
Nate Chapin | 71da03c | 2019-02-05 01:21:41 | [diff] [blame] | 26 | #include "content/public/common/content_features.h" |
creis | 0b49fa1 | 2017-07-10 16:31:11 | [diff] [blame] | 27 | #include "content/public/common/content_switches.h" |
[email protected] | a1d2916 | 2011-10-14 17:14:03 | [diff] [blame] | 28 | #include "content/public/common/url_constants.h" |
clamy | 7fced7b | 2017-11-16 19:52:43 | [diff] [blame] | 29 | #include "content/public/common/url_utils.h" |
[email protected] | be28b5f4 | 2012-07-20 11:31:25 | [diff] [blame] | 30 | #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 31 | |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 32 | namespace content { |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 33 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 34 | int32_t SiteInstanceImpl::next_site_instance_id_ = 1; |
[email protected] | 992db4c | 2011-05-12 15:37:15 | [diff] [blame] | 35 | |
Aaron Colwell | 6fee3f5 | 2019-05-08 21:35:02 | [diff] [blame] | 36 | // static |
| 37 | const GURL& SiteInstanceImpl::GetDefaultSiteURL() { |
| 38 | struct DefaultSiteURL { |
| 39 | const GURL url = GURL("http://unisolated.invalid"); |
| 40 | }; |
| 41 | static base::LazyInstance<DefaultSiteURL>::Leaky default_site_url = |
| 42 | LAZY_INSTANCE_INITIALIZER; |
| 43 | |
| 44 | return default_site_url.Get().url; |
| 45 | } |
| 46 | |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 47 | SiteInstanceImpl::SiteInstanceImpl(BrowsingInstance* browsing_instance) |
[email protected] | 992db4c | 2011-05-12 15:37:15 | [diff] [blame] | 48 | : id_(next_site_instance_id_++), |
creis | cce56cd | 2014-09-29 22:45:22 | [diff] [blame] | 49 | active_frame_count_(0), |
[email protected] | 992db4c | 2011-05-12 15:37:15 | [diff] [blame] | 50 | browsing_instance_(browsing_instance), |
nick | d5bbd0b | 2016-03-31 19:52:44 | [diff] [blame] | 51 | process_(nullptr), |
Lukasz Anforowicz | 3caa837 | 2018-06-05 17:22:07 | [diff] [blame] | 52 | can_associate_with_spare_process_(true), |
nick | d5bbd0b | 2016-03-31 19:52:44 | [diff] [blame] | 53 | has_site_(false), |
Tsuyoshi Horo | eb576e6 | 2017-06-28 06:00:41 | [diff] [blame] | 54 | process_reuse_policy_(ProcessReusePolicy::DEFAULT), |
| 55 | is_for_service_worker_(false) { |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 56 | DCHECK(browsing_instance); |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 57 | } |
| 58 | |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 59 | SiteInstanceImpl::~SiteInstanceImpl() { |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 60 | GetContentClient()->browser()->SiteInstanceDeleting(this); |
[email protected] | 056ad2a | 2011-07-12 02:13:55 | [diff] [blame] | 61 | |
Charlie Reis | cf6aa51 | 2019-04-16 17:27:29 | [diff] [blame] | 62 | if (process_) { |
[email protected] | 40bfaf3 | 2013-11-19 01:35:43 | [diff] [blame] | 63 | process_->RemoveObserver(this); |
| 64 | |
Charlie Reis | cf6aa51 | 2019-04-16 17:27:29 | [diff] [blame] | 65 | // Ensure the RenderProcessHost gets deleted if this SiteInstance created a |
| 66 | // process which was never used by any listeners. |
| 67 | process_->Cleanup(); |
| 68 | } |
| 69 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 70 | // Now that no one is referencing us, we can safely remove ourselves from |
| 71 | // the BrowsingInstance. Any future visits to a page from this site |
| 72 | // (within the same BrowsingInstance) can safely create a new SiteInstance. |
| 73 | if (has_site_) |
dcheng | bccd6b8 | 2016-03-30 16:24:19 | [diff] [blame] | 74 | browsing_instance_->UnregisterSiteInstance(this); |
| 75 | } |
| 76 | |
Nasko Oskov | deb6c7ea | 2017-11-30 18:18:11 | [diff] [blame] | 77 | // static |
dcheng | bccd6b8 | 2016-03-30 16:24:19 | [diff] [blame] | 78 | scoped_refptr<SiteInstanceImpl> SiteInstanceImpl::Create( |
| 79 | BrowserContext* browser_context) { |
Lukasz Anforowicz | 4726a17 | 2018-10-15 21:25:10 | [diff] [blame] | 80 | DCHECK(browser_context); |
kylechar | da69d88 | 2017-10-04 05:49:52 | [diff] [blame] | 81 | return base::WrapRefCounted( |
dcheng | bccd6b8 | 2016-03-30 16:24:19 | [diff] [blame] | 82 | new SiteInstanceImpl(new BrowsingInstance(browser_context))); |
| 83 | } |
| 84 | |
Nasko Oskov | deb6c7ea | 2017-11-30 18:18:11 | [diff] [blame] | 85 | // static |
dcheng | bccd6b8 | 2016-03-30 16:24:19 | [diff] [blame] | 86 | scoped_refptr<SiteInstanceImpl> SiteInstanceImpl::CreateForURL( |
| 87 | BrowserContext* browser_context, |
| 88 | const GURL& url) { |
Lukasz Anforowicz | 4726a17 | 2018-10-15 21:25:10 | [diff] [blame] | 89 | DCHECK(browser_context); |
dcheng | bccd6b8 | 2016-03-30 16:24:19 | [diff] [blame] | 90 | // This will create a new SiteInstance and BrowsingInstance. |
| 91 | scoped_refptr<BrowsingInstance> instance( |
| 92 | new BrowsingInstance(browser_context)); |
Aaron Colwell | ddeccbdb | 2019-03-08 01:11:03 | [diff] [blame] | 93 | return instance->GetSiteInstanceForURL(url, |
| 94 | /* allow_default_instance */ false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 95 | } |
| 96 | |
Nasko Oskov | deb6c7ea | 2017-11-30 18:18:11 | [diff] [blame] | 97 | // static |
Aaron Colwell | ecc72fb | 2019-04-30 17:41:11 | [diff] [blame] | 98 | scoped_refptr<SiteInstanceImpl> SiteInstanceImpl::CreateForServiceWorker( |
| 99 | BrowserContext* browser_context, |
| 100 | const GURL& url) { |
| 101 | // This will create a new SiteInstance and BrowsingInstance. |
| 102 | scoped_refptr<BrowsingInstance> instance( |
| 103 | new BrowsingInstance(browser_context)); |
| 104 | |
| 105 | // We do NOT want to allow the default site instance here because workers |
| 106 | // need to be kept separate from other sites. |
| 107 | scoped_refptr<SiteInstanceImpl> site_instance = |
| 108 | instance->GetSiteInstanceForURL(url, /* allow_default_instance */ false); |
| 109 | site_instance->is_for_service_worker_ = true; |
| 110 | return site_instance; |
| 111 | } |
| 112 | |
| 113 | // static |
Nasko Oskov | deb6c7ea | 2017-11-30 18:18:11 | [diff] [blame] | 114 | bool SiteInstanceImpl::ShouldAssignSiteForURL(const GURL& url) { |
| 115 | // about:blank should not "use up" a new SiteInstance. The SiteInstance can |
| 116 | // still be used for a normal web site. |
Mohamed Abdelhalim | 9e868ca4 | 2019-04-25 16:53:29 | [diff] [blame] | 117 | if (url.IsAboutBlank()) |
Nasko Oskov | deb6c7ea | 2017-11-30 18:18:11 | [diff] [blame] | 118 | return false; |
| 119 | |
| 120 | // The embedder will then have the opportunity to determine if the URL |
| 121 | // should "use up" the SiteInstance. |
| 122 | return GetContentClient()->browser()->ShouldAssignSiteForURL(url); |
| 123 | } |
| 124 | |
Nicolas Pena | 7c7847f | 2018-05-30 01:36:05 | [diff] [blame] | 125 | // static |
| 126 | bool SiteInstanceImpl::IsOriginLockASite(const GURL& lock_url) { |
| 127 | return lock_url.has_scheme() && lock_url.has_host(); |
| 128 | } |
| 129 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 130 | int32_t SiteInstanceImpl::GetId() { |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 131 | return id_; |
| 132 | } |
| 133 | |
Alex Moshchuk | 8e5c195 | 2019-01-15 03:39:50 | [diff] [blame] | 134 | const IsolationContext& SiteInstanceImpl::GetIsolationContext() { |
| 135 | return browsing_instance_->isolation_context(); |
| 136 | } |
| 137 | |
Nate Chapin | 71da03c | 2019-02-05 01:21:41 | [diff] [blame] | 138 | RenderProcessHost* SiteInstanceImpl::GetDefaultProcessIfUsable() { |
| 139 | if (!base::FeatureList::IsEnabled( |
| 140 | features::kProcessSharingWithStrictSiteInstances)) { |
| 141 | return nullptr; |
| 142 | } |
| 143 | if (RequiresDedicatedProcess()) |
| 144 | return nullptr; |
| 145 | return browsing_instance_->default_process(); |
| 146 | } |
| 147 | |
Aaron Colwell | ddeccbdb | 2019-03-08 01:11:03 | [diff] [blame] | 148 | bool SiteInstanceImpl::IsDefaultSiteInstance() { |
| 149 | return browsing_instance_->IsDefaultSiteInstance(this); |
| 150 | } |
| 151 | |
Nate Chapin | 71da03c | 2019-02-05 01:21:41 | [diff] [blame] | 152 | void SiteInstanceImpl::MaybeSetBrowsingInstanceDefaultProcess() { |
| 153 | if (!base::FeatureList::IsEnabled( |
| 154 | features::kProcessSharingWithStrictSiteInstances)) { |
| 155 | return; |
| 156 | } |
| 157 | // Wait until this SiteInstance both has a site and a process |
| 158 | // assigned, so that we can be sure that RequiresDedicatedProcess() |
| 159 | // is accurate and we actually have a process to set. |
| 160 | if (!process_ || !has_site_ || RequiresDedicatedProcess()) |
| 161 | return; |
| 162 | if (browsing_instance_->default_process()) { |
| 163 | DCHECK_EQ(process_, browsing_instance_->default_process()); |
| 164 | return; |
| 165 | } |
| 166 | browsing_instance_->SetDefaultProcess(process_); |
| 167 | } |
| 168 | |
Alex Moshchuk | 8e5c195 | 2019-01-15 03:39:50 | [diff] [blame] | 169 | // static |
| 170 | BrowsingInstanceId SiteInstanceImpl::NextBrowsingInstanceId() { |
| 171 | return BrowsingInstance::NextBrowsingInstanceId(); |
| 172 | } |
| 173 | |
| 174 | bool SiteInstanceImpl::HasProcess() { |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 175 | if (process_ != nullptr) |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 176 | return true; |
| 177 | |
| 178 | // If we would use process-per-site for this site, also check if there is an |
| 179 | // existing process that we would use if GetProcess() were called. |
Alex Moshchuk | f748879 | 2019-03-11 22:37:57 | [diff] [blame] | 180 | BrowserContext* browser_context = browsing_instance_->GetBrowserContext(); |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 181 | if (has_site_ && |
[email protected] | 1ae93fb1 | 2013-06-14 03:38:56 | [diff] [blame] | 182 | RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_) && |
Alex Moshchuk | 8e5c195 | 2019-01-15 03:39:50 | [diff] [blame] | 183 | RenderProcessHostImpl::GetSoleProcessHostForSite( |
| 184 | browser_context, GetIsolationContext(), site_, lock_url_)) { |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 185 | return true; |
| 186 | } |
| 187 | |
| 188 | return false; |
[email protected] | 5ab79b0 | 2010-04-26 16:47:11 | [diff] [blame] | 189 | } |
| 190 | |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 191 | RenderProcessHost* SiteInstanceImpl::GetProcess() { |
[email protected] | 08c8ec7e | 2010-07-11 17:14:48 | [diff] [blame] | 192 | // TODO(erikkay) It would be nice to ensure that the renderer type had been |
| 193 | // properly set before we get here. The default tab creation case winds up |
| 194 | // with no site set at this point, so it will default to TYPE_NORMAL. This |
| 195 | // may not be correct, so we'll wind up potentially creating a process that |
| 196 | // we then throw away, or worse sharing a process with the wrong process type. |
| 197 | // See crbug.com/43448. |
| 198 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 199 | // Create a new process if ours went away or was reused. |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 200 | if (!process_) { |
Alex Moshchuk | f748879 | 2019-03-11 22:37:57 | [diff] [blame] | 201 | BrowserContext* browser_context = browsing_instance_->GetBrowserContext(); |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 202 | |
clamy | 63960c3 | 2017-05-10 22:57:07 | [diff] [blame] | 203 | // Check if the ProcessReusePolicy should be updated. |
| 204 | bool should_use_process_per_site = |
creis | a9f04ba | 2017-05-19 22:27:42 | [diff] [blame] | 205 | has_site_ && |
| 206 | RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_); |
clamy | 63960c3 | 2017-05-10 22:57:07 | [diff] [blame] | 207 | if (should_use_process_per_site) { |
| 208 | process_reuse_policy_ = ProcessReusePolicy::PROCESS_PER_SITE; |
| 209 | } else if (process_reuse_policy_ == ProcessReusePolicy::PROCESS_PER_SITE) { |
| 210 | process_reuse_policy_ = ProcessReusePolicy::DEFAULT; |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 211 | } |
| 212 | |
Alex Moshchuk | 5f926a5 | 2018-08-29 20:57:30 | [diff] [blame] | 213 | process_ = RenderProcessHostImpl::GetProcessHostForSiteInstance(this); |
avi | 6d686db | 2017-01-06 02:28:57 | [diff] [blame] | 214 | |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 215 | CHECK(process_); |
[email protected] | 40bfaf3 | 2013-11-19 01:35:43 | [diff] [blame] | 216 | process_->AddObserver(this); |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 217 | |
Nate Chapin | 71da03c | 2019-02-05 01:21:41 | [diff] [blame] | 218 | MaybeSetBrowsingInstanceDefaultProcess(); |
| 219 | |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 220 | // If we are using process-per-site, we need to register this process |
| 221 | // for the current site so that we can find it again. (If no site is set |
| 222 | // at this time, we will register it in SetSite().) |
clamy | 63960c3 | 2017-05-10 22:57:07 | [diff] [blame] | 223 | if (process_reuse_policy_ == ProcessReusePolicy::PROCESS_PER_SITE && |
| 224 | has_site_) { |
Alex Moshchuk | 5f926a5 | 2018-08-29 20:57:30 | [diff] [blame] | 225 | RenderProcessHostImpl::RegisterSoleProcessHostForSite(browser_context, |
| 226 | process_, this); |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 227 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 228 | |
nasko | b8744d2 | 2014-08-28 17:07:43 | [diff] [blame] | 229 | TRACE_EVENT2("navigation", "SiteInstanceImpl::GetProcess", |
| 230 | "site id", id_, "process id", process_->GetID()); |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 231 | GetContentClient()->browser()->SiteInstanceGotProcess(this); |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 232 | |
[email protected] | 313b80bd | 2011-11-23 03:49:10 | [diff] [blame] | 233 | if (has_site_) |
alexmos | 13fe196 | 2017-06-28 04:25:12 | [diff] [blame] | 234 | LockToOriginIfNeeded(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 235 | } |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 236 | DCHECK(process_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 237 | |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 238 | return process_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 239 | } |
| 240 | |
Lukasz Anforowicz | 3caa837 | 2018-06-05 17:22:07 | [diff] [blame] | 241 | bool SiteInstanceImpl::CanAssociateWithSpareProcess() { |
| 242 | return can_associate_with_spare_process_; |
| 243 | } |
| 244 | |
| 245 | void SiteInstanceImpl::PreventAssociationWithSpareProcess() { |
| 246 | can_associate_with_spare_process_ = false; |
| 247 | } |
| 248 | |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 249 | void SiteInstanceImpl::SetSite(const GURL& url) { |
Charlie Reis | c209975 | 2019-04-06 01:21:12 | [diff] [blame] | 250 | // TODO(creis): Consider calling ShouldAssignSiteForURL internally, rather |
| 251 | // than before multiple call sites. See https://crbug.com/949220. |
nasko | b8744d2 | 2014-08-28 17:07:43 | [diff] [blame] | 252 | TRACE_EVENT2("navigation", "SiteInstanceImpl::SetSite", |
| 253 | "site id", id_, "url", url.possibly_invalid_spec()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 254 | // A SiteInstance's site should not change. |
| 255 | // TODO(creis): When following links or script navigations, we can currently |
| 256 | // render pages from other sites in this SiteInstance. This will eventually |
| 257 | // be fixed, but until then, we should still not set the site of a |
| 258 | // SiteInstance more than once. |
| 259 | DCHECK(!has_site_); |
| 260 | |
| 261 | // Remember that this SiteInstance has been used to load a URL, even if the |
| 262 | // URL is invalid. |
| 263 | has_site_ = true; |
Alex Moshchuk | f748879 | 2019-03-11 22:37:57 | [diff] [blame] | 264 | BrowserContext* browser_context = browsing_instance_->GetBrowserContext(); |
Alex Moshchuk | 25c64bb | 2017-12-02 02:50:11 | [diff] [blame] | 265 | original_url_ = url; |
Aaron Colwell | ddeccbdb | 2019-03-08 01:11:03 | [diff] [blame] | 266 | browsing_instance_->GetSiteAndLockForURL( |
| 267 | url, /* allow_default_instance */ false, &site_, &lock_url_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 268 | |
| 269 | // Now that we have a site, register it with the BrowsingInstance. This |
| 270 | // ensures that we won't create another SiteInstance for this site within |
| 271 | // the same BrowsingInstance, because all same-site pages within a |
| 272 | // BrowsingInstance can script each other. |
| 273 | browsing_instance_->RegisterSiteInstance(this); |
[email protected] | 313b80bd | 2011-11-23 03:49:10 | [diff] [blame] | 274 | |
clamy | 63960c3 | 2017-05-10 22:57:07 | [diff] [blame] | 275 | // Update the process reuse policy based on the site. |
| 276 | bool should_use_process_per_site = |
| 277 | RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_); |
| 278 | if (should_use_process_per_site) { |
| 279 | process_reuse_policy_ = ProcessReusePolicy::PROCESS_PER_SITE; |
| 280 | } |
| 281 | |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 282 | if (process_) { |
alexmos | 13fe196 | 2017-06-28 04:25:12 | [diff] [blame] | 283 | LockToOriginIfNeeded(); |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 284 | |
| 285 | // Ensure the process is registered for this site if necessary. |
clamy | 63960c3 | 2017-05-10 22:57:07 | [diff] [blame] | 286 | if (should_use_process_per_site) { |
Alex Moshchuk | 5f926a5 | 2018-08-29 20:57:30 | [diff] [blame] | 287 | RenderProcessHostImpl::RegisterSoleProcessHostForSite(browser_context, |
| 288 | process_, this); |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 289 | } |
Nate Chapin | 71da03c | 2019-02-05 01:21:41 | [diff] [blame] | 290 | MaybeSetBrowsingInstanceDefaultProcess(); |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 291 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 292 | } |
| 293 | |
Lucas Furukawa Gadani | d726e1e | 2019-05-08 16:20:03 | [diff] [blame] | 294 | const GURL& SiteInstanceImpl::GetSiteURL() { |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 295 | return site_; |
| 296 | } |
| 297 | |
| 298 | bool SiteInstanceImpl::HasSite() const { |
| 299 | return has_site_; |
| 300 | } |
| 301 | |
| 302 | bool SiteInstanceImpl::HasRelatedSiteInstance(const GURL& url) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 303 | return browsing_instance_->HasSiteInstance(url); |
| 304 | } |
| 305 | |
dcheng | bccd6b8 | 2016-03-30 16:24:19 | [diff] [blame] | 306 | scoped_refptr<SiteInstance> SiteInstanceImpl::GetRelatedSiteInstance( |
| 307 | const GURL& url) { |
Aaron Colwell | ddeccbdb | 2019-03-08 01:11:03 | [diff] [blame] | 308 | return browsing_instance_->GetSiteInstanceForURL( |
| 309 | url, /* allow_default_instance */ true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 310 | } |
| 311 | |
[email protected] | 14392a5 | 2012-05-02 20:28:44 | [diff] [blame] | 312 | bool SiteInstanceImpl::IsRelatedSiteInstance(const SiteInstance* instance) { |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 313 | return browsing_instance_.get() == static_cast<const SiteInstanceImpl*>( |
| 314 | instance)->browsing_instance_.get(); |
[email protected] | 14392a5 | 2012-05-02 20:28:44 | [diff] [blame] | 315 | } |
| 316 | |
[email protected] | d5072a8 | 2014-05-15 05:50:18 | [diff] [blame] | 317 | size_t SiteInstanceImpl::GetRelatedActiveContentsCount() { |
| 318 | return browsing_instance_->active_contents_count(); |
| 319 | } |
| 320 | |
[email protected] | f88628d0 | 2012-11-11 17:58:59 | [diff] [blame] | 321 | bool SiteInstanceImpl::HasWrongProcessForURL(const GURL& url) { |
[email protected] | 144a810 | 2012-01-14 01:05:31 | [diff] [blame] | 322 | // If the URL to navigate to can be associated with any site instance, |
| 323 | // we want to keep it in the same process. |
[email protected] | c02f1ba | 2014-02-03 06:53:53 | [diff] [blame] | 324 | if (IsRendererDebugURL(url)) |
[email protected] | 144a810 | 2012-01-14 01:05:31 | [diff] [blame] | 325 | return false; |
| 326 | |
Nasko Oskov | 978c16b | 2018-08-03 22:17:06 | [diff] [blame] | 327 | // Any process can host an about:blank URL, except the one used for error |
| 328 | // pages, which should not commit successful navigations. This check avoids a |
| 329 | // process transfer for browser-initiated navigations to about:blank in a |
| 330 | // dedicated process; without it, IsSuitableHost would consider this process |
| 331 | // unsuitable for about:blank when it compares origin locks. |
| 332 | // Renderer-initiated navigations will handle about:blank navigations |
| 333 | // elsewhere and leave them in the source SiteInstance, along with |
| 334 | // about:srcdoc and data:. |
| 335 | if (url.IsAboutBlank() && site_ != GURL(kUnreachableWebDataURL)) |
alexmos | 13fe196 | 2017-06-28 04:25:12 | [diff] [blame] | 336 | return false; |
| 337 | |
[email protected] | 88aae97 | 2011-12-16 01:14:18 | [diff] [blame] | 338 | // If the site URL is an extension (e.g., for hosted apps or WebUI) but the |
[email protected] | d292d8a | 2011-05-25 03:47:11 | [diff] [blame] | 339 | // process is not (or vice versa), make sure we notice and fix it. |
Aaron Colwell | ddeccbdb | 2019-03-08 01:11:03 | [diff] [blame] | 340 | GURL site_url; |
| 341 | GURL origin_lock; |
| 342 | |
| 343 | // Note: This call must return information that is identical to what |
| 344 | // would be reported in the SiteInstance returned by |
| 345 | // GetRelatedSiteInstance(url). |
| 346 | browsing_instance_->GetSiteAndLockForURL( |
| 347 | url, /* allow_default_instance */ true, &site_url, &origin_lock); |
Charlie Reis | c209975 | 2019-04-06 01:21:12 | [diff] [blame] | 348 | |
| 349 | // Note that HasProcess() may return true if process_ is null, in |
| 350 | // process-per-site cases where there's an existing process available. |
| 351 | // We want to use such a process in the IsSuitableHost check, so we |
| 352 | // may end up assigning process_ in the GetProcess() call below. |
| 353 | if (!HasProcess()) { |
| 354 | // If there is no process or site, then this is a new SiteInstance that can |
| 355 | // be used for anything. |
| 356 | if (!HasSite()) |
| 357 | return false; |
| 358 | |
| 359 | // If there is no process but there is a site, then the process must have |
| 360 | // been discarded after we navigated away. If the site URLs match, then it |
| 361 | // is safe to use this SiteInstance. |
| 362 | if (GetSiteURL() == site_url) |
| 363 | return false; |
| 364 | |
| 365 | // If the site URLs do not match, but neither this SiteInstance nor the |
| 366 | // destination site_url require dedicated processes, then it is safe to use |
| 367 | // this SiteInstance. |
| 368 | if (!RequiresDedicatedProcess() && |
Aaron Colwell | 5080de6 | 2019-05-06 23:02:03 | [diff] [blame] | 369 | !DoesSiteURLRequireDedicatedProcess(GetIsolationContext(), site_url)) { |
Charlie Reis | c209975 | 2019-04-06 01:21:12 | [diff] [blame] | 370 | return false; |
| 371 | } |
| 372 | |
| 373 | // Otherwise, there's no process, the site URLs don't match, and at least |
| 374 | // one of them requires a dedicated process, so it is not safe to use this |
| 375 | // SiteInstance. |
| 376 | return true; |
| 377 | } |
| 378 | |
[email protected] | 88aae97 | 2011-12-16 01:14:18 | [diff] [blame] | 379 | return !RenderProcessHostImpl::IsSuitableHost( |
Alex Moshchuk | f748879 | 2019-03-11 22:37:57 | [diff] [blame] | 380 | GetProcess(), browsing_instance_->GetBrowserContext(), |
Alex Moshchuk | 8e5c195 | 2019-01-15 03:39:50 | [diff] [blame] | 381 | GetIsolationContext(), site_url, origin_lock); |
[email protected] | d292d8a | 2011-05-25 03:47:11 | [diff] [blame] | 382 | } |
| 383 | |
nick | d30fd96 | 2015-07-27 21:51:08 | [diff] [blame] | 384 | bool SiteInstanceImpl::RequiresDedicatedProcess() { |
| 385 | if (!has_site_) |
| 386 | return false; |
nick | d5bbd0b | 2016-03-31 19:52:44 | [diff] [blame] | 387 | |
Aaron Colwell | 5080de6 | 2019-05-06 23:02:03 | [diff] [blame] | 388 | return DoesSiteURLRequireDedicatedProcess(GetIsolationContext(), site_); |
nick | d30fd96 | 2015-07-27 21:51:08 | [diff] [blame] | 389 | } |
| 390 | |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 391 | void SiteInstanceImpl::IncrementActiveFrameCount() { |
| 392 | active_frame_count_++; |
| 393 | } |
| 394 | |
| 395 | void SiteInstanceImpl::DecrementActiveFrameCount() { |
ericwilligers | 254597b | 2016-10-17 10:32:31 | [diff] [blame] | 396 | if (--active_frame_count_ == 0) { |
| 397 | for (auto& observer : observers_) |
| 398 | observer.ActiveFrameCountIsZero(this); |
| 399 | } |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 400 | } |
| 401 | |
[email protected] | d5072a8 | 2014-05-15 05:50:18 | [diff] [blame] | 402 | void SiteInstanceImpl::IncrementRelatedActiveContentsCount() { |
| 403 | browsing_instance_->increment_active_contents_count(); |
| 404 | } |
| 405 | |
| 406 | void SiteInstanceImpl::DecrementRelatedActiveContentsCount() { |
| 407 | browsing_instance_->decrement_active_contents_count(); |
| 408 | } |
| 409 | |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 410 | void SiteInstanceImpl::AddObserver(Observer* observer) { |
| 411 | observers_.AddObserver(observer); |
| 412 | } |
| 413 | |
| 414 | void SiteInstanceImpl::RemoveObserver(Observer* observer) { |
| 415 | observers_.RemoveObserver(observer); |
| 416 | } |
| 417 | |
Lucas Furukawa Gadani | d726e1e | 2019-05-08 16:20:03 | [diff] [blame] | 418 | BrowserContext* SiteInstanceImpl::GetBrowserContext() { |
Alex Moshchuk | f748879 | 2019-03-11 22:37:57 | [diff] [blame] | 419 | return browsing_instance_->GetBrowserContext(); |
[email protected] | 72daaa9 | 2012-01-18 13:39:02 | [diff] [blame] | 420 | } |
| 421 | |
nick | cc0d914 | 2015-10-14 16:27:10 | [diff] [blame] | 422 | // static |
dcheng | bccd6b8 | 2016-03-30 16:24:19 | [diff] [blame] | 423 | scoped_refptr<SiteInstance> SiteInstance::Create( |
| 424 | BrowserContext* browser_context) { |
Lukasz Anforowicz | 4726a17 | 2018-10-15 21:25:10 | [diff] [blame] | 425 | DCHECK(browser_context); |
dcheng | bccd6b8 | 2016-03-30 16:24:19 | [diff] [blame] | 426 | return SiteInstanceImpl::Create(browser_context); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 427 | } |
| 428 | |
nick | cc0d914 | 2015-10-14 16:27:10 | [diff] [blame] | 429 | // static |
dcheng | bccd6b8 | 2016-03-30 16:24:19 | [diff] [blame] | 430 | scoped_refptr<SiteInstance> SiteInstance::CreateForURL( |
| 431 | BrowserContext* browser_context, |
| 432 | const GURL& url) { |
Lukasz Anforowicz | 4726a17 | 2018-10-15 21:25:10 | [diff] [blame] | 433 | DCHECK(browser_context); |
dcheng | bccd6b8 | 2016-03-30 16:24:19 | [diff] [blame] | 434 | return SiteInstanceImpl::CreateForURL(browser_context, url); |
[email protected] | d8a9662 | 2009-05-07 19:21:16 | [diff] [blame] | 435 | } |
| 436 | |
nick | cc0d914 | 2015-10-14 16:27:10 | [diff] [blame] | 437 | // static |
Nasko Oskov | deb6c7ea | 2017-11-30 18:18:11 | [diff] [blame] | 438 | bool SiteInstance::ShouldAssignSiteForURL(const GURL& url) { |
| 439 | return SiteInstanceImpl::ShouldAssignSiteForURL(url); |
| 440 | } |
| 441 | |
Alex Moshchuk | 78cf66bda | 2018-11-30 01:49:30 | [diff] [blame] | 442 | bool SiteInstanceImpl::IsSameSiteWithURL(const GURL& url) { |
Aaron Colwell | 6fee3f5 | 2019-05-08 21:35:02 | [diff] [blame] | 443 | if (IsDefaultSiteInstance()) { |
| 444 | // Consider |url| the same site if it could be handled by the |
| 445 | // default SiteInstance and we don't already have a SiteInstance for |
| 446 | // this URL. |
| 447 | // TODO(acolwell): Remove HasSiteInstance() call once we have a way to |
| 448 | // prevent SiteInstances with no site URL from being used for URLs |
| 449 | // that should be routed to the default SiteInstance. |
| 450 | DCHECK_EQ(site_, GetDefaultSiteURL()); |
| 451 | return site_ == GetSiteForURLInternal(GetIsolationContext(), url, |
| 452 | true /* should_use_effective_urls */, |
| 453 | true /* allow_default_site_url */) && |
| 454 | !browsing_instance_->HasSiteInstance(url); |
| 455 | } |
| 456 | |
Alex Moshchuk | 78cf66bda | 2018-11-30 01:49:30 | [diff] [blame] | 457 | return SiteInstanceImpl::IsSameWebSite( |
Lukasz Anforowicz | 1a0a89a | 2019-03-29 22:31:02 | [diff] [blame] | 458 | GetIsolationContext(), site_, url, |
| 459 | true /* should_compare_effective_urls */); |
Alex Moshchuk | 25c64bb | 2017-12-02 02:50:11 | [diff] [blame] | 460 | } |
| 461 | |
Aaron Colwell | 6fee3f5 | 2019-05-08 21:35:02 | [diff] [blame] | 462 | bool SiteInstanceImpl::IsOriginalUrlSameSite( |
| 463 | const GURL& dest_url, |
| 464 | bool should_compare_effective_urls) { |
| 465 | if (IsDefaultSiteInstance()) |
| 466 | return IsSameSiteWithURL(dest_url); |
| 467 | |
| 468 | return IsSameWebSite(GetIsolationContext(), original_url_, dest_url, |
| 469 | should_compare_effective_urls); |
| 470 | } |
| 471 | |
Alex Moshchuk | 78cf66bda | 2018-11-30 01:49:30 | [diff] [blame] | 472 | // static |
Lukasz Anforowicz | 1a0a89a | 2019-03-29 22:31:02 | [diff] [blame] | 473 | bool SiteInstanceImpl::IsSameWebSite(const IsolationContext& isolation_context, |
Alex Moshchuk | 25c64bb | 2017-12-02 02:50:11 | [diff] [blame] | 474 | const GURL& real_src_url, |
| 475 | const GURL& real_dest_url, |
| 476 | bool should_compare_effective_urls) { |
Lukasz Anforowicz | 1a0a89a | 2019-03-29 22:31:02 | [diff] [blame] | 477 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 478 | BrowserContext* browser_context = |
| 479 | isolation_context.browser_or_resource_context().ToBrowserContext(); |
Lukasz Anforowicz | 4726a17 | 2018-10-15 21:25:10 | [diff] [blame] | 480 | DCHECK(browser_context); |
Aaron Colwell | 6fee3f5 | 2019-05-08 21:35:02 | [diff] [blame] | 481 | DCHECK_NE(real_src_url, GetDefaultSiteURL()); |
Lukasz Anforowicz | 4726a17 | 2018-10-15 21:25:10 | [diff] [blame] | 482 | |
Alex Moshchuk | 25c64bb | 2017-12-02 02:50:11 | [diff] [blame] | 483 | GURL src_url = |
| 484 | should_compare_effective_urls |
| 485 | ? SiteInstanceImpl::GetEffectiveURL(browser_context, real_src_url) |
| 486 | : real_src_url; |
| 487 | GURL dest_url = |
| 488 | should_compare_effective_urls |
| 489 | ? SiteInstanceImpl::GetEffectiveURL(browser_context, real_dest_url) |
| 490 | : real_dest_url; |
[email protected] | 399583b | 2012-12-11 09:33:42 | [diff] [blame] | 491 | |
| 492 | // We infer web site boundaries based on the registered domain name of the |
| 493 | // top-level page and the scheme. We do not pay attention to the port if |
| 494 | // one is present, because pages served from different ports can still |
| 495 | // access each other if they change their document.domain variable. |
| 496 | |
| 497 | // Some special URLs will match the site instance of any other URL. This is |
| 498 | // done before checking both of them for validity, since we want these URLs |
| 499 | // to have the same site instance as even an invalid one. |
[email protected] | 855d7d57 | 2014-08-02 11:18:17 | [diff] [blame] | 500 | if (IsRendererDebugURL(src_url) || IsRendererDebugURL(dest_url)) |
[email protected] | 399583b | 2012-12-11 09:33:42 | [diff] [blame] | 501 | return true; |
| 502 | |
| 503 | // If either URL is invalid, they aren't part of the same site. |
[email protected] | 855d7d57 | 2014-08-02 11:18:17 | [diff] [blame] | 504 | if (!src_url.is_valid() || !dest_url.is_valid()) |
[email protected] | 399583b | 2012-12-11 09:33:42 | [diff] [blame] | 505 | return false; |
| 506 | |
[email protected] | 855d7d57 | 2014-08-02 11:18:17 | [diff] [blame] | 507 | // If the destination url is just a blank page, we treat them as part of the |
| 508 | // same site. |
Mohamed Abdelhalim | 9e868ca4 | 2019-04-25 16:53:29 | [diff] [blame] | 509 | if (dest_url.IsAboutBlank()) |
[email protected] | 855d7d57 | 2014-08-02 11:18:17 | [diff] [blame] | 510 | return true; |
| 511 | |
Alex Moshchuk | a308c9b | 2018-02-08 20:58:14 | [diff] [blame] | 512 | // If the source and destination URLs are equal excluding the hash, they have |
| 513 | // the same site. This matters for file URLs, where SameDomainOrHost() would |
| 514 | // otherwise return false below. |
| 515 | if (src_url.EqualsIgnoringRef(dest_url)) |
| 516 | return true; |
| 517 | |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 518 | url::Origin src_origin = url::Origin::Create(src_url); |
| 519 | url::Origin dest_origin = url::Origin::Create(dest_url); |
alexmos | 3b9ad10 | 2017-05-26 23:41:08 | [diff] [blame] | 520 | |
[email protected] | 399583b | 2012-12-11 09:33:42 | [diff] [blame] | 521 | // If the schemes differ, they aren't part of the same site. |
alexmos | cbf99578 | 2017-06-01 03:13:13 | [diff] [blame] | 522 | if (src_origin.scheme() != dest_origin.scheme()) |
[email protected] | 399583b | 2012-12-11 09:33:42 | [diff] [blame] | 523 | return false; |
| 524 | |
W. James MacLean | f79c97e | 2019-05-02 20:35:46 | [diff] [blame] | 525 | if (SiteIsolationPolicy::IsStrictOriginIsolationEnabled()) |
| 526 | return src_origin == dest_origin; |
| 527 | |
alexmos | 4bc2632 | 2017-07-01 00:57:14 | [diff] [blame] | 528 | if (!net::registry_controlled_domains::SameDomainOrHost( |
| 529 | src_origin, dest_origin, |
| 530 | net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) { |
| 531 | return false; |
| 532 | } |
| 533 | |
| 534 | // If the sites are the same, check isolated origins. If either URL matches |
Alex Moshchuk | 4e19b36 | 2018-09-10 21:14:36 | [diff] [blame] | 535 | // an isolated origin, compare origins rather than sites. As an optimization |
| 536 | // to avoid unneeded isolated origin lookups, shortcut this check if the two |
| 537 | // origins are the same. |
| 538 | if (src_origin == dest_origin) |
| 539 | return true; |
alexmos | 4bc2632 | 2017-07-01 00:57:14 | [diff] [blame] | 540 | auto* policy = ChildProcessSecurityPolicyImpl::GetInstance(); |
| 541 | url::Origin src_isolated_origin; |
| 542 | url::Origin dest_isolated_origin; |
Alex Moshchuk | 8e5c195 | 2019-01-15 03:39:50 | [diff] [blame] | 543 | bool src_origin_is_isolated = policy->GetMatchingIsolatedOrigin( |
| 544 | isolation_context, src_origin, &src_isolated_origin); |
| 545 | bool dest_origin_is_isolated = policy->GetMatchingIsolatedOrigin( |
| 546 | isolation_context, dest_origin, &dest_isolated_origin); |
alexmos | 4bc2632 | 2017-07-01 00:57:14 | [diff] [blame] | 547 | if (src_origin_is_isolated || dest_origin_is_isolated) { |
| 548 | // Compare most specific matching origins to ensure that a subdomain of an |
| 549 | // isolated origin (e.g., https://subdomain.isolated.foo.com) also matches |
| 550 | // the isolated origin's site URL (e.g., https://isolated.foo.com). |
| 551 | return src_isolated_origin == dest_isolated_origin; |
| 552 | } |
| 553 | |
| 554 | return true; |
[email protected] | 399583b | 2012-12-11 09:33:42 | [diff] [blame] | 555 | } |
| 556 | |
nick | cc0d914 | 2015-10-14 16:27:10 | [diff] [blame] | 557 | // static |
[email protected] | 399583b | 2012-12-11 09:33:42 | [diff] [blame] | 558 | GURL SiteInstance::GetSiteForURL(BrowserContext* browser_context, |
Alex Moshchuk | 5f926a5 | 2018-08-29 20:57:30 | [diff] [blame] | 559 | const GURL& url) { |
Aaron Colwell | ea6921f | 2019-01-29 16:50:39 | [diff] [blame] | 560 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 561 | DCHECK(browser_context); |
| 562 | |
Aaron Colwell | 3acea72 | 2019-04-24 21:54:43 | [diff] [blame] | 563 | // By default, GetSiteForURL will resolve |url| to an effective URL |
| 564 | // before computing its site. |
Alex Moshchuk | 8e5c195 | 2019-01-15 03:39:50 | [diff] [blame] | 565 | // |
| 566 | // TODO(alexmos): Callers inside content/ should already be using the |
| 567 | // internal SiteInstanceImpl version and providing a proper IsolationContext. |
| 568 | // For callers outside content/, plumb the applicable IsolationContext here, |
| 569 | // where needed. Eventually, GetSiteForURL should always require an |
| 570 | // IsolationContext to be passed in, and this implementation should just |
| 571 | // become SiteInstanceImpl::GetSiteForURL. |
Aaron Colwell | 3acea72 | 2019-04-24 21:54:43 | [diff] [blame] | 572 | return SiteInstanceImpl::GetSiteForURL(IsolationContext(browser_context), |
| 573 | url); |
Alex Moshchuk | 5f926a5 | 2018-08-29 20:57:30 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | // static |
Alex Moshchuk | 8e5c195 | 2019-01-15 03:39:50 | [diff] [blame] | 577 | GURL SiteInstanceImpl::DetermineProcessLockURL( |
Alex Moshchuk | 8e5c195 | 2019-01-15 03:39:50 | [diff] [blame] | 578 | const IsolationContext& isolation_context, |
| 579 | const GURL& url) { |
Alex Moshchuk | 5f926a5 | 2018-08-29 20:57:30 | [diff] [blame] | 580 | // For the process lock URL, convert |url| to a site without resolving |url| |
| 581 | // to an effective URL. |
Aaron Colwell | 3acea72 | 2019-04-24 21:54:43 | [diff] [blame] | 582 | return SiteInstanceImpl::GetSiteForURLInternal( |
Aaron Colwell | 6fee3f5 | 2019-05-08 21:35:02 | [diff] [blame] | 583 | isolation_context, url, false /* should_use_effective_urls */, |
| 584 | false /* allow_default_site_url */); |
Alex Moshchuk | 5f926a5 | 2018-08-29 20:57:30 | [diff] [blame] | 585 | } |
| 586 | |
Aaron Colwell | ea6921f | 2019-01-29 16:50:39 | [diff] [blame] | 587 | // static |
Alex Moshchuk | f748879 | 2019-03-11 22:37:57 | [diff] [blame] | 588 | GURL SiteInstanceImpl::GetSiteForURL(const IsolationContext& isolation_context, |
Aaron Colwell | 3acea72 | 2019-04-24 21:54:43 | [diff] [blame] | 589 | const GURL& real_url) { |
| 590 | return GetSiteForURLInternal(isolation_context, real_url, |
Aaron Colwell | 6fee3f5 | 2019-05-08 21:35:02 | [diff] [blame] | 591 | true /* should_use_effective_urls */, |
| 592 | false /* allow_default_site_url */); |
Aaron Colwell | 3acea72 | 2019-04-24 21:54:43 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | // static |
| 596 | GURL SiteInstanceImpl::GetSiteForURLInternal( |
| 597 | const IsolationContext& isolation_context, |
| 598 | const GURL& real_url, |
Aaron Colwell | 6fee3f5 | 2019-05-08 21:35:02 | [diff] [blame] | 599 | bool should_use_effective_urls, |
| 600 | bool allow_default_site_url) { |
[email protected] | 25a9e8e53 | 2012-06-22 06:16:28 | [diff] [blame] | 601 | // TODO(fsamuel, creis): For some reason appID is not recognized as a host. |
[email protected] | 6eb1a11e | 2013-10-09 00:54:37 | [diff] [blame] | 602 | if (real_url.SchemeIs(kGuestScheme)) |
[email protected] | 25a9e8e53 | 2012-06-22 06:16:28 | [diff] [blame] | 603 | return real_url; |
| 604 | |
Aaron Colwell | ea6921f | 2019-01-29 16:50:39 | [diff] [blame] | 605 | if (should_use_effective_urls) |
| 606 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 607 | |
Alex Moshchuk | 5f926a5 | 2018-08-29 20:57:30 | [diff] [blame] | 608 | GURL url = should_use_effective_urls |
Alex Moshchuk | f748879 | 2019-03-11 22:37:57 | [diff] [blame] | 609 | ? SiteInstanceImpl::GetEffectiveURL( |
| 610 | isolation_context.browser_or_resource_context() |
| 611 | .ToBrowserContext(), |
| 612 | real_url) |
Alex Moshchuk | 5f926a5 | 2018-08-29 20:57:30 | [diff] [blame] | 613 | : real_url; |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 614 | url::Origin origin = url::Origin::Create(url); |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 615 | |
Lukasz Anforowicz | 48097c4 | 2017-12-15 00:23:38 | [diff] [blame] | 616 | // If the url has a host, then determine the site. Skip file URLs to avoid a |
| 617 | // situation where site URL of file://localhost/ would mismatch Blink's origin |
| 618 | // (which ignores the hostname in this case - see https://crbug.com/776160). |
Aaron Colwell | 6fee3f5 | 2019-05-08 21:35:02 | [diff] [blame] | 619 | GURL site_url; |
Lukasz Anforowicz | 48097c4 | 2017-12-15 00:23:38 | [diff] [blame] | 620 | if (!origin.host().empty() && origin.scheme() != url::kFileScheme) { |
W. James MacLean | f79c97e | 2019-05-02 20:35:46 | [diff] [blame] | 621 | if (SiteIsolationPolicy::IsStrictOriginIsolationEnabled()) |
| 622 | return origin.GetURL(); |
| 623 | |
Aaron Colwell | 6fee3f5 | 2019-05-08 21:35:02 | [diff] [blame] | 624 | site_url = GetSiteForOrigin(origin); |
Alex Moshchuk | 4e19b36 | 2018-09-10 21:14:36 | [diff] [blame] | 625 | |
| 626 | // Isolated origins should use the full origin as their site URL. A |
| 627 | // subdomain of an isolated origin should also use that isolated origin's |
| 628 | // site URL. It is important to check |origin| (based on |url|) rather than |
| 629 | // |real_url| here, since some effective URLs (such as for NTP) need to be |
| 630 | // resolved prior to the isolated origin lookup. |
| 631 | auto* policy = ChildProcessSecurityPolicyImpl::GetInstance(); |
| 632 | url::Origin isolated_origin; |
Alex Moshchuk | 8e5c195 | 2019-01-15 03:39:50 | [diff] [blame] | 633 | if (policy->GetMatchingIsolatedOrigin(isolation_context, origin, site_url, |
| 634 | &isolated_origin)) |
Alex Moshchuk | 4e19b36 | 2018-09-10 21:14:36 | [diff] [blame] | 635 | return isolated_origin.GetURL(); |
Alex Moshchuk | 5f926a5 | 2018-08-29 20:57:30 | [diff] [blame] | 636 | |
| 637 | // If an effective URL was used, augment the effective site URL with the |
| 638 | // underlying web site in the hash. This is needed to keep |
| 639 | // navigations across sites covered by one hosted app in separate |
| 640 | // SiteInstances. See https://crbug.com/791796. |
| 641 | // |
| 642 | // TODO(https://crbug.com/734722): Consider replacing this hack with |
| 643 | // a proper security principal. |
| 644 | if (should_use_effective_urls && url != real_url) { |
Alex Moshchuk | 4e19b36 | 2018-09-10 21:14:36 | [diff] [blame] | 645 | std::string non_translated_site_url( |
Aaron Colwell | 3acea72 | 2019-04-24 21:54:43 | [diff] [blame] | 646 | GetSiteForURLInternal(isolation_context, real_url, |
Aaron Colwell | 6fee3f5 | 2019-05-08 21:35:02 | [diff] [blame] | 647 | false /* should_use_effective_urls */, |
| 648 | allow_default_site_url) |
Alex Moshchuk | 4e19b36 | 2018-09-10 21:14:36 | [diff] [blame] | 649 | .spec()); |
| 650 | GURL::Replacements replacements; |
| 651 | replacements.SetRefStr(non_translated_site_url.c_str()); |
| 652 | site_url = site_url.ReplaceComponents(replacements); |
Alex Moshchuk | 5f926a5 | 2018-08-29 20:57:30 | [diff] [blame] | 653 | } |
Aaron Colwell | 6fee3f5 | 2019-05-08 21:35:02 | [diff] [blame] | 654 | } else { |
| 655 | // If there is no host but there is a scheme, return the scheme. |
| 656 | // This is useful for cases like file URLs. |
| 657 | if (!origin.opaque()) { |
| 658 | // Prefer to use the scheme of |origin| rather than |url|, to correctly |
| 659 | // cover blob:file: and filesystem:file: URIs (see also |
| 660 | // https://crbug.com/697111). |
| 661 | DCHECK(!origin.scheme().empty()); |
| 662 | site_url = GURL(origin.scheme() + ":"); |
| 663 | } else if (url.has_scheme()) { |
| 664 | // In some cases, it is not safe to use just the scheme as a site URL, as |
| 665 | // that might allow two URLs created by different sites to share a |
| 666 | // process. See https://crbug.com/863623 and https://crbug.com/863069. |
| 667 | // |
| 668 | // TODO(alexmos,creis): This should eventually be expanded to certain |
| 669 | // other schemes, such as file:. |
| 670 | // TODO(creis): This currently causes problems with tests on Android and |
| 671 | // Android WebView. For now, skip it when Site Isolation is not enabled, |
| 672 | // since there's no need to isolate data and blob URLs from each other in |
| 673 | // that case. |
| 674 | bool is_site_isolation_enabled = |
| 675 | SiteIsolationPolicy::UseDedicatedProcessesForAllSites() || |
| 676 | SiteIsolationPolicy::AreIsolatedOriginsEnabled(); |
| 677 | if (is_site_isolation_enabled && |
| 678 | (url.SchemeIsBlob() || url.scheme() == url::kDataScheme)) { |
| 679 | // We get here for blob URLs of form blob:null/guid. Use the full URL |
| 680 | // with the guid in that case, which isolates all blob URLs with unique |
| 681 | // origins from each other. We also get here for browser-initiated |
| 682 | // navigations to data URLs, which have a unique origin and should only |
| 683 | // share a process when they are identical. Remove hash from the URL in |
| 684 | // either case, since same-document navigations shouldn't use a |
| 685 | // different site URL. |
| 686 | if (url.has_ref()) { |
| 687 | GURL::Replacements replacements; |
| 688 | replacements.ClearRef(); |
| 689 | url = url.ReplaceComponents(replacements); |
| 690 | } |
| 691 | site_url = url; |
| 692 | } else { |
| 693 | DCHECK(!url.scheme().empty()); |
| 694 | site_url = GURL(url.scheme() + ":"); |
Alex Moshchuk | b1f8748 | 2018-07-19 01:51:51 | [diff] [blame] | 695 | } |
Aaron Colwell | 6fee3f5 | 2019-05-08 21:35:02 | [diff] [blame] | 696 | } else { |
| 697 | // Otherwise the URL should be invalid; return an empty site. |
| 698 | DCHECK(!url.is_valid()) << url; |
| 699 | return GURL(); |
Alex Moshchuk | b1f8748 | 2018-07-19 01:51:51 | [diff] [blame] | 700 | } |
Lukasz Anforowicz | d926a84 | 2018-03-09 01:50:45 | [diff] [blame] | 701 | } |
creis | f60c2cd | 2014-12-18 00:41:02 | [diff] [blame] | 702 | |
Aaron Colwell | 6fee3f5 | 2019-05-08 21:35:02 | [diff] [blame] | 703 | if (allow_default_site_url && |
| 704 | !base::FeatureList::IsEnabled( |
| 705 | features::kProcessSharingWithStrictSiteInstances) && |
| 706 | !DoesSiteURLRequireDedicatedProcess(isolation_context, site_url)) { |
| 707 | return GetDefaultSiteURL(); |
| 708 | } |
| 709 | return site_url; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 710 | } |
| 711 | |
nick | cc0d914 | 2015-10-14 16:27:10 | [diff] [blame] | 712 | // static |
Alex Moshchuk | 4e19b36 | 2018-09-10 21:14:36 | [diff] [blame] | 713 | GURL SiteInstanceImpl::GetSiteForOrigin(const url::Origin& origin) { |
| 714 | // Only keep the scheme and registered domain of |origin|. |
| 715 | std::string domain = net::registry_controlled_domains::GetDomainAndRegistry( |
| 716 | origin.host(), |
| 717 | net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
| 718 | std::string site = origin.scheme(); |
| 719 | site += url::kStandardSchemeSeparator; |
| 720 | site += domain.empty() ? origin.host() : domain; |
| 721 | return GURL(site); |
| 722 | } |
| 723 | |
| 724 | // static |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 725 | GURL SiteInstanceImpl::GetEffectiveURL(BrowserContext* browser_context, |
| 726 | const GURL& url) { |
Aaron Colwell | ea6921f | 2019-01-29 16:50:39 | [diff] [blame] | 727 | DCHECK(browser_context); |
Alex Moshchuk | a31c788 | 2018-01-17 00:57:30 | [diff] [blame] | 728 | return GetContentClient()->browser()->GetEffectiveURL(browser_context, url); |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 729 | } |
| 730 | |
nick | cc0d914 | 2015-10-14 16:27:10 | [diff] [blame] | 731 | // static |
Alex Moshchuk | 25c64bb | 2017-12-02 02:50:11 | [diff] [blame] | 732 | bool SiteInstanceImpl::HasEffectiveURL(BrowserContext* browser_context, |
| 733 | const GURL& url) { |
| 734 | return GetEffectiveURL(browser_context, url) != url; |
| 735 | } |
| 736 | |
| 737 | // static |
nick | cc0d914 | 2015-10-14 16:27:10 | [diff] [blame] | 738 | bool SiteInstanceImpl::DoesSiteRequireDedicatedProcess( |
Alex Moshchuk | 8e5c195 | 2019-01-15 03:39:50 | [diff] [blame] | 739 | const IsolationContext& isolation_context, |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 740 | const GURL& url) { |
Aaron Colwell | 5080de6 | 2019-05-06 23:02:03 | [diff] [blame] | 741 | return SiteIsolationPolicy::UseDedicatedProcessesForAllSites() || |
| 742 | DoesSiteURLRequireDedicatedProcess( |
| 743 | isolation_context, |
| 744 | SiteInstanceImpl::GetSiteForURL(isolation_context, url)); |
| 745 | } |
| 746 | |
| 747 | // static |
| 748 | bool SiteInstanceImpl::DoesSiteURLRequireDedicatedProcess( |
| 749 | const IsolationContext& isolation_context, |
| 750 | const GURL& site_url) { |
Lukasz Anforowicz | 4aff3b8 | 2019-04-04 16:00:33 | [diff] [blame] | 751 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO) || |
| 752 | BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 753 | DCHECK(isolation_context.browser_or_resource_context()); |
Lukasz Anforowicz | 4726a17 | 2018-10-15 21:25:10 | [diff] [blame] | 754 | |
nick | cc0d914 | 2015-10-14 16:27:10 | [diff] [blame] | 755 | // If --site-per-process is enabled, site isolation is enabled everywhere. |
| 756 | if (SiteIsolationPolicy::UseDedicatedProcessesForAllSites()) |
| 757 | return true; |
| 758 | |
alexmos | 3b9ad10 | 2017-05-26 23:41:08 | [diff] [blame] | 759 | // Always require a dedicated process for isolated origins. |
alexmos | 3b9ad10 | 2017-05-26 23:41:08 | [diff] [blame] | 760 | auto* policy = ChildProcessSecurityPolicyImpl::GetInstance(); |
Alex Moshchuk | 8e5c195 | 2019-01-15 03:39:50 | [diff] [blame] | 761 | if (policy->IsIsolatedOrigin(isolation_context, |
| 762 | url::Origin::Create(site_url))) |
alexmos | 3b9ad10 | 2017-05-26 23:41:08 | [diff] [blame] | 763 | return true; |
| 764 | |
Lukasz Anforowicz | 1f5ad40 | 2018-09-28 23:57:54 | [diff] [blame] | 765 | // Error pages in main frames do require isolation, however since this is |
| 766 | // missing the context whether this is for a main frame or not, that part |
| 767 | // is enforced in RenderFrameHostManager. |
| 768 | if (site_url.SchemeIs(kChromeErrorScheme)) |
| 769 | return true; |
| 770 | |
| 771 | // Isolate kChromeUIScheme pages from one another and from other kinds of |
| 772 | // schemes. |
| 773 | if (site_url.SchemeIs(content::kChromeUIScheme)) |
| 774 | return true; |
| 775 | |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 776 | // Let the content embedder enable site isolation for specific URLs. Use the |
| 777 | // canonical site url for this check, so that schemes with nested origins |
| 778 | // (blob and filesystem) work properly. |
Avi Drissman | 8c36df7 | 2017-12-11 21:19:20 | [diff] [blame] | 779 | if (GetContentClient()->browser()->DoesSiteRequireDedicatedProcess( |
Lukasz Anforowicz | 4aff3b8 | 2019-04-04 16:00:33 | [diff] [blame] | 780 | isolation_context.browser_or_resource_context(), site_url)) { |
nick | cc0d914 | 2015-10-14 16:27:10 | [diff] [blame] | 781 | return true; |
| 782 | } |
| 783 | |
| 784 | return false; |
| 785 | } |
| 786 | |
alexmos | 13fe196 | 2017-06-28 04:25:12 | [diff] [blame] | 787 | // static |
Alex Moshchuk | 8e5c195 | 2019-01-15 03:39:50 | [diff] [blame] | 788 | bool SiteInstanceImpl::ShouldLockToOrigin( |
Alex Moshchuk | 8e5c195 | 2019-01-15 03:39:50 | [diff] [blame] | 789 | const IsolationContext& isolation_context, |
| 790 | GURL site_url) { |
Lukasz Anforowicz | 1a0a89a | 2019-03-29 22:31:02 | [diff] [blame] | 791 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 792 | BrowserContext* browser_context = |
| 793 | isolation_context.browser_or_resource_context().ToBrowserContext(); |
Lukasz Anforowicz | 4726a17 | 2018-10-15 21:25:10 | [diff] [blame] | 794 | DCHECK(browser_context); |
| 795 | |
creis | 0b49fa1 | 2017-07-10 16:31:11 | [diff] [blame] | 796 | // Don't lock to origin in --single-process mode, since this mode puts |
| 797 | // cross-site pages into the same process. |
Alex Moshchuk | d4c4ca3 | 2018-08-15 20:36:51 | [diff] [blame] | 798 | if (RenderProcessHost::run_renderer_in_process()) |
creis | 0b49fa1 | 2017-07-10 16:31:11 | [diff] [blame] | 799 | return false; |
| 800 | |
Aaron Colwell | 5080de6 | 2019-05-06 23:02:03 | [diff] [blame] | 801 | if (!DoesSiteURLRequireDedicatedProcess(isolation_context, site_url)) |
alexmos | 13fe196 | 2017-06-28 04:25:12 | [diff] [blame] | 802 | return false; |
| 803 | |
| 804 | // Guest processes cannot be locked to their site because guests always have |
| 805 | // a fixed SiteInstance. The site of GURLs a guest loads doesn't match that |
| 806 | // SiteInstance. So we skip locking the guest process to the site. |
| 807 | // TODO(ncarter): Remove this exclusion once we can make origin lock per |
| 808 | // RenderFrame routing id. |
| 809 | if (site_url.SchemeIs(content::kGuestScheme)) |
| 810 | return false; |
| 811 | |
alexmos | 13fe196 | 2017-06-28 04:25:12 | [diff] [blame] | 812 | // TODO(creis, nick): Until we can handle sites with effective URLs at the |
| 813 | // call sites of ChildProcessSecurityPolicy::CanAccessDataForOrigin, we |
| 814 | // must give the embedder a chance to exempt some sites to avoid process |
| 815 | // kills. |
| 816 | if (!GetContentClient()->browser()->ShouldLockToOrigin(browser_context, |
| 817 | site_url)) { |
| 818 | return false; |
| 819 | } |
| 820 | |
| 821 | return true; |
| 822 | } |
| 823 | |
Lukasz Anforowicz | 27851e6 | 2018-12-18 23:04:08 | [diff] [blame] | 824 | // static |
| 825 | base::Optional<url::Origin> SiteInstanceImpl::GetRequestInitiatorSiteLock( |
Lukasz Anforowicz | 27851e6 | 2018-12-18 23:04:08 | [diff] [blame] | 826 | GURL site_url) { |
Lukasz Anforowicz | 27851e6 | 2018-12-18 23:04:08 | [diff] [blame] | 827 | // The following schemes are safe for sites that require a process lock: |
| 828 | // - data: - locking |request_initiator| to an opaque origin |
| 829 | // - http/https - requiring |request_initiator| to match |site_url| with |
| 830 | // DomainIs (i.e. suffix-based) comparison. |
| 831 | if (site_url.SchemeIsHTTPOrHTTPS() || site_url.SchemeIs(url::kDataScheme)) |
| 832 | return url::Origin::Create(site_url); |
| 833 | |
| 834 | // Other schemes might not be safe to use as |request_initiator_site_lock|. |
| 835 | // One example is chrome-guest://... |
| 836 | return base::nullopt; |
| 837 | } |
| 838 | |
[email protected] | 40bfaf3 | 2013-11-19 01:35:43 | [diff] [blame] | 839 | void SiteInstanceImpl::RenderProcessHostDestroyed(RenderProcessHost* host) { |
| 840 | DCHECK_EQ(process_, host); |
| 841 | process_->RemoveObserver(this); |
nick | d5bbd0b | 2016-03-31 19:52:44 | [diff] [blame] | 842 | process_ = nullptr; |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 843 | } |
[email protected] | 313b80bd | 2011-11-23 03:49:10 | [diff] [blame] | 844 | |
Bo Liu | 2a48940 | 2018-04-24 23:41:27 | [diff] [blame] | 845 | void SiteInstanceImpl::RenderProcessExited( |
| 846 | RenderProcessHost* host, |
| 847 | const ChildProcessTerminationInfo& info) { |
ericwilligers | 254597b | 2016-10-17 10:32:31 | [diff] [blame] | 848 | for (auto& observer : observers_) |
| 849 | observer.RenderProcessGone(this); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 850 | } |
| 851 | |
alexmos | 13fe196 | 2017-06-28 04:25:12 | [diff] [blame] | 852 | void SiteInstanceImpl::LockToOriginIfNeeded() { |
| 853 | DCHECK(HasSite()); |
| 854 | |
| 855 | // From now on, this process should be considered "tainted" for future |
| 856 | // process reuse decisions: |
| 857 | // (1) If |site_| required a dedicated process, this SiteInstance's process |
| 858 | // can only host URLs for the same site. |
| 859 | // (2) Even if |site_| does not require a dedicated process, this |
| 860 | // SiteInstance's process still cannot be reused to host other sites |
| 861 | // requiring dedicated sites in the future. |
| 862 | // We can get here either when we commit a URL into a SiteInstance that does |
| 863 | // not yet have a site, or when we create a process for a SiteInstance with a |
| 864 | // preassigned site. |
| 865 | process_->SetIsUsed(); |
| 866 | |
Alex Moshchuk | 47dd00f | 2017-10-10 17:15:23 | [diff] [blame] | 867 | ChildProcessSecurityPolicyImpl* policy = |
| 868 | ChildProcessSecurityPolicyImpl::GetInstance(); |
Alex Moshchuk | 58225c8 | 2019-04-18 00:45:01 | [diff] [blame] | 869 | GURL process_lock = policy->GetOriginLock(process_->GetID()); |
Lukasz Anforowicz | 1a0a89a | 2019-03-29 22:31:02 | [diff] [blame] | 870 | if (ShouldLockToOrigin(GetIsolationContext(), site_)) { |
Alex Moshchuk | 9cf5a45d | 2017-08-14 18:50:57 | [diff] [blame] | 871 | // Sanity check that this won't try to assign an origin lock to a <webview> |
| 872 | // process, which can't be locked. |
| 873 | CHECK(!process_->IsForGuestsOnly()); |
| 874 | |
Alex Moshchuk | 58225c8 | 2019-04-18 00:45:01 | [diff] [blame] | 875 | if (process_lock.is_empty()) { |
| 876 | // TODO(nick): When all sites are isolated, this operation provides |
| 877 | // strong protection. If only some sites are isolated, we need |
| 878 | // additional logic to prevent the non-isolated sites from requesting |
| 879 | // resources for isolated sites. https://crbug.com/509125 |
| 880 | TRACE_EVENT2("navigation", "SiteInstanceImpl::LockToOrigin", "site id", |
| 881 | id_, "lock", lock_url().possibly_invalid_spec()); |
| 882 | process_->LockToOrigin(GetIsolationContext(), lock_url()); |
| 883 | } else if (process_lock != lock_url()) { |
| 884 | // We should never attempt to reassign a different origin lock to a |
| 885 | // process. |
| 886 | base::debug::SetCrashKeyString(bad_message::GetRequestedSiteURLKey(), |
| 887 | site_.spec()); |
| 888 | base::debug::SetCrashKeyString( |
| 889 | bad_message::GetKilledProcessOriginLockKey(), process_lock.spec()); |
| 890 | CHECK(false) << "Trying to lock a process to " << lock_url() |
| 891 | << " but the process is already locked to " << process_lock; |
| 892 | } else { |
| 893 | // Process already has the right origin lock assigned. This case will |
| 894 | // happen for commits to |site_| after the first one. |
Alex Moshchuk | 9cf5a45d | 2017-08-14 18:50:57 | [diff] [blame] | 895 | } |
Alex Moshchuk | 47dd00f | 2017-10-10 17:15:23 | [diff] [blame] | 896 | } else { |
| 897 | // If the site that we've just committed doesn't require a dedicated |
| 898 | // process, make sure we aren't putting it in a process for a site that |
| 899 | // does. |
Alex Moshchuk | 58225c8 | 2019-04-18 00:45:01 | [diff] [blame] | 900 | if (!process_lock.is_empty()) { |
Alex Moshchuk | 1656c67 | 2018-04-28 01:48:34 | [diff] [blame] | 901 | base::debug::SetCrashKeyString(bad_message::GetRequestedSiteURLKey(), |
| 902 | site_.spec()); |
| 903 | base::debug::SetCrashKeyString( |
Alex Moshchuk | 58225c8 | 2019-04-18 00:45:01 | [diff] [blame] | 904 | bad_message::GetKilledProcessOriginLockKey(), process_lock.spec()); |
Alex Moshchuk | 1656c67 | 2018-04-28 01:48:34 | [diff] [blame] | 905 | CHECK(false) << "Trying to commit non-isolated site " << site_ |
Alex Moshchuk | 58225c8 | 2019-04-18 00:45:01 | [diff] [blame] | 906 | << " in process locked to " << process_lock; |
Alex Moshchuk | 1656c67 | 2018-04-28 01:48:34 | [diff] [blame] | 907 | } |
[email protected] | 313b80bd | 2011-11-23 03:49:10 | [diff] [blame] | 908 | } |
| 909 | } |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 910 | |
Alex Moshchuk | 07e1bb4 | 2019-03-08 04:44:08 | [diff] [blame] | 911 | // static |
| 912 | void SiteInstance::StartIsolatingSite(BrowserContext* context, |
| 913 | const GURL& url) { |
| 914 | if (!SiteIsolationPolicy::AreDynamicIsolatedOriginsEnabled()) |
| 915 | return; |
| 916 | |
| 917 | // Ignore attempts to isolate origins that are not supported. Do this here |
| 918 | // instead of relying on AddIsolatedOrigins()'s internal validation, to avoid |
| 919 | // the runtime warning generated by the latter. |
| 920 | url::Origin origin(url::Origin::Create(url)); |
| 921 | if (!IsolatedOriginUtil::IsValidIsolatedOrigin(origin)) |
| 922 | return; |
| 923 | |
| 924 | // Convert |url| to a site, to avoid breaking document.domain. Note that |
| 925 | // this doesn't use effective URL resolution or other special cases from |
| 926 | // GetSiteForURL() and simply converts |origin| to a scheme and eTLD+1. |
| 927 | GURL site(SiteInstanceImpl::GetSiteForOrigin(origin)); |
| 928 | |
| 929 | ChildProcessSecurityPolicyImpl* policy = |
| 930 | ChildProcessSecurityPolicyImpl::GetInstance(); |
Alex Moshchuk | e256d56 | 2019-04-26 21:43:57 | [diff] [blame] | 931 | url::Origin site_origin(url::Origin::Create(site)); |
| 932 | policy->AddIsolatedOrigins({site_origin}, context); |
| 933 | |
| 934 | // This function currently assumes the new isolated site should persist |
| 935 | // across restarts, so ask the embedder to save it, excluding off-the-record |
| 936 | // profiles. |
| 937 | if (!context->IsOffTheRecord()) |
| 938 | GetContentClient()->browser()->PersistIsolatedOrigin(context, site_origin); |
Alex Moshchuk | 07e1bb4 | 2019-03-08 04:44:08 | [diff] [blame] | 939 | } |
| 940 | |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 941 | } // namespace content |