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