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