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