blob: e283790d3c5ec2b9406d12cc872bb93f78205290 [file] [log] [blame]
[email protected]144a8102012-01-14 01:05:311// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]b6583592012-01-25 19:52:335#include "content/browser/site_instance_impl.h"
initial.commit09911bf2008-07-26 23:55:296
Lukasz Anforowicz48097c42017-12-15 00:23:387#include <string>
8
creis0b49fa12017-07-10 16:31:119#include "base/command_line.h"
Alex Moshchuk4479b97c2017-10-17 23:20:3210#include "base/debug/crash_logging.h"
alexmos3b9ad102017-05-26 23:41:0811#include "base/macros.h"
[email protected]39365212011-02-24 01:01:0012#include "content/browser/browsing_instance.h"
[email protected]b9535422012-02-09 01:47:5913#include "content/browser/child_process_security_policy_impl.h"
[email protected]c02f1ba2014-02-03 06:53:5314#include "content/browser/frame_host/debug_urls.h"
nick9f34e2892016-01-12 21:01:0715#include "content/browser/frame_host/frame_tree_node.h"
[email protected]f3b1a082011-11-18 00:34:3016#include "content/browser/renderer_host/render_process_host_impl.h"
[email protected]4c3a23582012-08-18 08:54:3417#include "content/browser/storage_partition_impl.h"
[email protected]87f3c082011-10-19 18:07:4418#include "content/public/browser/content_browser_client.h"
[email protected]f3b1a082011-11-18 00:34:3019#include "content/public/browser/render_process_host_factory.h"
Nick Carterbf6264a52018-04-06 02:39:3320#include "content/public/browser/site_isolation_policy.h"
[email protected]41fb79a52012-06-29 16:34:3321#include "content/public/browser/web_ui_controller_factory.h"
creis0b49fa12017-07-10 16:31:1122#include "content/public/common/content_switches.h"
[email protected]a1d29162011-10-14 17:14:0323#include "content/public/common/url_constants.h"
clamy7fced7b2017-11-16 19:52:4324#include "content/public/common/url_utils.h"
[email protected]be28b5f42012-07-20 11:31:2525#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
initial.commit09911bf2008-07-26 23:55:2926
[email protected]46488322012-10-30 03:22:2027namespace content {
[email protected]b6583592012-01-25 19:52:3328
avib7348942015-12-25 20:57:1029int32_t SiteInstanceImpl::next_site_instance_id_ = 1;
[email protected]992db4c2011-05-12 15:37:1530
Alex Moshchuk1656c672018-04-28 01:48:3431using CheckOriginLockResult =
32 ChildProcessSecurityPolicyImpl::CheckOriginLockResult;
33
[email protected]b6583592012-01-25 19:52:3334SiteInstanceImpl::SiteInstanceImpl(BrowsingInstance* browsing_instance)
[email protected]992db4c2011-05-12 15:37:1535 : id_(next_site_instance_id_++),
creiscce56cd2014-09-29 22:45:2236 active_frame_count_(0),
[email protected]992db4c2011-05-12 15:37:1537 browsing_instance_(browsing_instance),
nickd5bbd0b2016-03-31 19:52:4438 process_(nullptr),
Lukasz Anforowicz3caa8372018-06-05 17:22:0739 can_associate_with_spare_process_(true),
nickd5bbd0b2016-03-31 19:52:4440 has_site_(false),
Tsuyoshi Horoeb576e62017-06-28 06:00:4141 process_reuse_policy_(ProcessReusePolicy::DEFAULT),
42 is_for_service_worker_(false) {
[email protected]4566f132009-03-12 01:55:1343 DCHECK(browsing_instance);
[email protected]4566f132009-03-12 01:55:1344}
45
[email protected]b6583592012-01-25 19:52:3346SiteInstanceImpl::~SiteInstanceImpl() {
[email protected]46488322012-10-30 03:22:2047 GetContentClient()->browser()->SiteInstanceDeleting(this);
[email protected]056ad2a2011-07-12 02:13:5548
[email protected]40bfaf32013-11-19 01:35:4349 if (process_)
50 process_->RemoveObserver(this);
51
initial.commit09911bf2008-07-26 23:55:2952 // Now that no one is referencing us, we can safely remove ourselves from
53 // the BrowsingInstance. Any future visits to a page from this site
54 // (within the same BrowsingInstance) can safely create a new SiteInstance.
55 if (has_site_)
dchengbccd6b82016-03-30 16:24:1956 browsing_instance_->UnregisterSiteInstance(this);
57}
58
Nasko Oskovdeb6c7ea2017-11-30 18:18:1159// static
dchengbccd6b82016-03-30 16:24:1960scoped_refptr<SiteInstanceImpl> SiteInstanceImpl::Create(
61 BrowserContext* browser_context) {
Lukasz Anforowicz4726a172018-10-15 21:25:1062 DCHECK(browser_context);
kylecharda69d882017-10-04 05:49:5263 return base::WrapRefCounted(
dchengbccd6b82016-03-30 16:24:1964 new SiteInstanceImpl(new BrowsingInstance(browser_context)));
65}
66
Nasko Oskovdeb6c7ea2017-11-30 18:18:1167// static
dchengbccd6b82016-03-30 16:24:1968scoped_refptr<SiteInstanceImpl> SiteInstanceImpl::CreateForURL(
69 BrowserContext* browser_context,
70 const GURL& url) {
Lukasz Anforowicz4726a172018-10-15 21:25:1071 DCHECK(browser_context);
dchengbccd6b82016-03-30 16:24:1972 // This will create a new SiteInstance and BrowsingInstance.
73 scoped_refptr<BrowsingInstance> instance(
74 new BrowsingInstance(browser_context));
75 return instance->GetSiteInstanceForURL(url);
initial.commit09911bf2008-07-26 23:55:2976}
77
Nasko Oskovdeb6c7ea2017-11-30 18:18:1178// static
79bool SiteInstanceImpl::ShouldAssignSiteForURL(const GURL& url) {
80 // about:blank should not "use up" a new SiteInstance. The SiteInstance can
81 // still be used for a normal web site.
82 if (url == url::kAboutBlankURL)
83 return false;
84
85 // The embedder will then have the opportunity to determine if the URL
86 // should "use up" the SiteInstance.
87 return GetContentClient()->browser()->ShouldAssignSiteForURL(url);
88}
89
Nicolas Pena7c7847f2018-05-30 01:36:0590// static
91bool SiteInstanceImpl::IsOriginLockASite(const GURL& lock_url) {
92 return lock_url.has_scheme() && lock_url.has_host();
93}
94
avib7348942015-12-25 20:57:1095int32_t SiteInstanceImpl::GetId() {
[email protected]b6583592012-01-25 19:52:3396 return id_;
97}
98
99bool SiteInstanceImpl::HasProcess() const {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28100 if (process_ != nullptr)
[email protected]41fb79a52012-06-29 16:34:33101 return true;
102
103 // If we would use process-per-site for this site, also check if there is an
104 // existing process that we would use if GetProcess() were called.
[email protected]46488322012-10-30 03:22:20105 BrowserContext* browser_context =
[email protected]41fb79a52012-06-29 16:34:33106 browsing_instance_->browser_context();
107 if (has_site_ &&
[email protected]1ae93fb12013-06-14 03:38:56108 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_) &&
Alex Moshchuk5f926a52018-08-29 20:57:30109 RenderProcessHostImpl::GetSoleProcessHostForSite(browser_context, site_,
110 lock_url_)) {
[email protected]41fb79a52012-06-29 16:34:33111 return true;
112 }
113
114 return false;
[email protected]5ab79b02010-04-26 16:47:11115}
116
[email protected]41fb79a52012-06-29 16:34:33117RenderProcessHost* SiteInstanceImpl::GetProcess() {
[email protected]08c8ec7e2010-07-11 17:14:48118 // TODO(erikkay) It would be nice to ensure that the renderer type had been
119 // properly set before we get here. The default tab creation case winds up
120 // with no site set at this point, so it will default to TYPE_NORMAL. This
121 // may not be correct, so we'll wind up potentially creating a process that
122 // we then throw away, or worse sharing a process with the wrong process type.
123 // See crbug.com/43448.
124
initial.commit09911bf2008-07-26 23:55:29125 // Create a new process if ours went away or was reused.
[email protected]4566f132009-03-12 01:55:13126 if (!process_) {
[email protected]4c3a23582012-08-18 08:54:34127 BrowserContext* browser_context = browsing_instance_->browser_context();
[email protected]41fb79a52012-06-29 16:34:33128
clamy63960c32017-05-10 22:57:07129 // Check if the ProcessReusePolicy should be updated.
130 bool should_use_process_per_site =
creisa9f04ba2017-05-19 22:27:42131 has_site_ &&
132 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_);
clamy63960c32017-05-10 22:57:07133 if (should_use_process_per_site) {
134 process_reuse_policy_ = ProcessReusePolicy::PROCESS_PER_SITE;
135 } else if (process_reuse_policy_ == ProcessReusePolicy::PROCESS_PER_SITE) {
136 process_reuse_policy_ = ProcessReusePolicy::DEFAULT;
[email protected]41fb79a52012-06-29 16:34:33137 }
138
Alex Moshchuk5f926a52018-08-29 20:57:30139 process_ = RenderProcessHostImpl::GetProcessHostForSiteInstance(this);
avi6d686db2017-01-06 02:28:57140
[email protected]41fb79a52012-06-29 16:34:33141 CHECK(process_);
[email protected]40bfaf32013-11-19 01:35:43142 process_->AddObserver(this);
[email protected]41fb79a52012-06-29 16:34:33143
144 // If we are using process-per-site, we need to register this process
145 // for the current site so that we can find it again. (If no site is set
146 // at this time, we will register it in SetSite().)
clamy63960c32017-05-10 22:57:07147 if (process_reuse_policy_ == ProcessReusePolicy::PROCESS_PER_SITE &&
148 has_site_) {
Alex Moshchuk5f926a52018-08-29 20:57:30149 RenderProcessHostImpl::RegisterSoleProcessHostForSite(browser_context,
150 process_, this);
[email protected]41fb79a52012-06-29 16:34:33151 }
initial.commit09911bf2008-07-26 23:55:29152
naskob8744d22014-08-28 17:07:43153 TRACE_EVENT2("navigation", "SiteInstanceImpl::GetProcess",
154 "site id", id_, "process id", process_->GetID());
[email protected]46488322012-10-30 03:22:20155 GetContentClient()->browser()->SiteInstanceGotProcess(this);
[email protected]6f371442011-11-09 06:45:46156
[email protected]313b80bd2011-11-23 03:49:10157 if (has_site_)
alexmos13fe1962017-06-28 04:25:12158 LockToOriginIfNeeded();
initial.commit09911bf2008-07-26 23:55:29159 }
[email protected]4566f132009-03-12 01:55:13160 DCHECK(process_);
initial.commit09911bf2008-07-26 23:55:29161
[email protected]4566f132009-03-12 01:55:13162 return process_;
initial.commit09911bf2008-07-26 23:55:29163}
164
Lukasz Anforowicz3caa8372018-06-05 17:22:07165bool SiteInstanceImpl::CanAssociateWithSpareProcess() {
166 return can_associate_with_spare_process_;
167}
168
169void SiteInstanceImpl::PreventAssociationWithSpareProcess() {
170 can_associate_with_spare_process_ = false;
171}
172
[email protected]b6583592012-01-25 19:52:33173void SiteInstanceImpl::SetSite(const GURL& url) {
naskob8744d22014-08-28 17:07:43174 TRACE_EVENT2("navigation", "SiteInstanceImpl::SetSite",
175 "site id", id_, "url", url.possibly_invalid_spec());
initial.commit09911bf2008-07-26 23:55:29176 // A SiteInstance's site should not change.
177 // TODO(creis): When following links or script navigations, we can currently
178 // render pages from other sites in this SiteInstance. This will eventually
179 // be fixed, but until then, we should still not set the site of a
180 // SiteInstance more than once.
181 DCHECK(!has_site_);
182
183 // Remember that this SiteInstance has been used to load a URL, even if the
184 // URL is invalid.
185 has_site_ = true;
[email protected]4c3a23582012-08-18 08:54:34186 BrowserContext* browser_context = browsing_instance_->browser_context();
Alex Moshchuk5f926a52018-08-29 20:57:30187 site_ =
188 GetSiteForURL(browser_context, url, true /* should_use_effective_urls */);
Alex Moshchuk25c64bb2017-12-02 02:50:11189 original_url_ = url;
Alex Moshchuk5f926a52018-08-29 20:57:30190 lock_url_ = DetermineProcessLockURL(browser_context, url);
initial.commit09911bf2008-07-26 23:55:29191
192 // Now that we have a site, register it with the BrowsingInstance. This
193 // ensures that we won't create another SiteInstance for this site within
194 // the same BrowsingInstance, because all same-site pages within a
195 // BrowsingInstance can script each other.
196 browsing_instance_->RegisterSiteInstance(this);
[email protected]313b80bd2011-11-23 03:49:10197
clamy63960c32017-05-10 22:57:07198 // Update the process reuse policy based on the site.
199 bool should_use_process_per_site =
200 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_);
201 if (should_use_process_per_site) {
202 process_reuse_policy_ = ProcessReusePolicy::PROCESS_PER_SITE;
203 }
204
[email protected]41fb79a52012-06-29 16:34:33205 if (process_) {
alexmos13fe1962017-06-28 04:25:12206 LockToOriginIfNeeded();
[email protected]41fb79a52012-06-29 16:34:33207
208 // Ensure the process is registered for this site if necessary.
clamy63960c32017-05-10 22:57:07209 if (should_use_process_per_site) {
Alex Moshchuk5f926a52018-08-29 20:57:30210 RenderProcessHostImpl::RegisterSoleProcessHostForSite(browser_context,
211 process_, this);
[email protected]41fb79a52012-06-29 16:34:33212 }
213 }
initial.commit09911bf2008-07-26 23:55:29214}
215
[email protected]77ab17312012-09-28 15:34:59216const GURL& SiteInstanceImpl::GetSiteURL() const {
[email protected]b6583592012-01-25 19:52:33217 return site_;
218}
219
220bool SiteInstanceImpl::HasSite() const {
221 return has_site_;
222}
223
224bool SiteInstanceImpl::HasRelatedSiteInstance(const GURL& url) {
initial.commit09911bf2008-07-26 23:55:29225 return browsing_instance_->HasSiteInstance(url);
226}
227
dchengbccd6b82016-03-30 16:24:19228scoped_refptr<SiteInstance> SiteInstanceImpl::GetRelatedSiteInstance(
229 const GURL& url) {
initial.commit09911bf2008-07-26 23:55:29230 return browsing_instance_->GetSiteInstanceForURL(url);
231}
232
[email protected]14392a52012-05-02 20:28:44233bool SiteInstanceImpl::IsRelatedSiteInstance(const SiteInstance* instance) {
[email protected]fc72bb12013-06-02 21:13:46234 return browsing_instance_.get() == static_cast<const SiteInstanceImpl*>(
235 instance)->browsing_instance_.get();
[email protected]14392a52012-05-02 20:28:44236}
237
[email protected]d5072a82014-05-15 05:50:18238size_t SiteInstanceImpl::GetRelatedActiveContentsCount() {
239 return browsing_instance_->active_contents_count();
240}
241
[email protected]f88628d02012-11-11 17:58:59242bool SiteInstanceImpl::HasWrongProcessForURL(const GURL& url) {
[email protected]d292d8a2011-05-25 03:47:11243 // Having no process isn't a problem, since we'll assign it correctly.
[email protected]f88628d02012-11-11 17:58:59244 // Note that HasProcess() may return true if process_ is null, in
245 // process-per-site cases where there's an existing process available.
246 // We want to use such a process in the IsSuitableHost check, so we
247 // may end up assigning process_ in the GetProcess() call below.
248 if (!HasProcess())
[email protected]d292d8a2011-05-25 03:47:11249 return false;
250
[email protected]144a8102012-01-14 01:05:31251 // If the URL to navigate to can be associated with any site instance,
252 // we want to keep it in the same process.
[email protected]c02f1ba2014-02-03 06:53:53253 if (IsRendererDebugURL(url))
[email protected]144a8102012-01-14 01:05:31254 return false;
255
Nasko Oskov978c16b2018-08-03 22:17:06256 // Any process can host an about:blank URL, except the one used for error
257 // pages, which should not commit successful navigations. This check avoids a
258 // process transfer for browser-initiated navigations to about:blank in a
259 // dedicated process; without it, IsSuitableHost would consider this process
260 // unsuitable for about:blank when it compares origin locks.
261 // Renderer-initiated navigations will handle about:blank navigations
262 // elsewhere and leave them in the source SiteInstance, along with
263 // about:srcdoc and data:.
264 if (url.IsAboutBlank() && site_ != GURL(kUnreachableWebDataURL))
alexmos13fe1962017-06-28 04:25:12265 return false;
266
[email protected]88aae972011-12-16 01:14:18267 // If the site URL is an extension (e.g., for hosted apps or WebUI) but the
[email protected]d292d8a2011-05-25 03:47:11268 // process is not (or vice versa), make sure we notice and fix it.
Alex Moshchuk5f926a52018-08-29 20:57:30269 GURL site_url =
270 SiteInstance::GetSiteForURL(browsing_instance_->browser_context(), url);
271 GURL origin_lock =
272 DetermineProcessLockURL(browsing_instance_->browser_context(), url);
[email protected]88aae972011-12-16 01:14:18273 return !RenderProcessHostImpl::IsSuitableHost(
Alex Moshchuk5f926a52018-08-29 20:57:30274 GetProcess(), browsing_instance_->browser_context(), site_url,
275 origin_lock);
[email protected]d292d8a2011-05-25 03:47:11276}
277
nickd30fd962015-07-27 21:51:08278bool SiteInstanceImpl::RequiresDedicatedProcess() {
279 if (!has_site_)
280 return false;
nickd5bbd0b2016-03-31 19:52:44281
282 return DoesSiteRequireDedicatedProcess(GetBrowserContext(), site_);
nickd30fd962015-07-27 21:51:08283}
284
nick9f34e2892016-01-12 21:01:07285void SiteInstanceImpl::IncrementActiveFrameCount() {
286 active_frame_count_++;
287}
288
289void SiteInstanceImpl::DecrementActiveFrameCount() {
ericwilligers254597b2016-10-17 10:32:31290 if (--active_frame_count_ == 0) {
291 for (auto& observer : observers_)
292 observer.ActiveFrameCountIsZero(this);
293 }
nick9f34e2892016-01-12 21:01:07294}
295
[email protected]d5072a82014-05-15 05:50:18296void SiteInstanceImpl::IncrementRelatedActiveContentsCount() {
297 browsing_instance_->increment_active_contents_count();
298}
299
300void SiteInstanceImpl::DecrementRelatedActiveContentsCount() {
301 browsing_instance_->decrement_active_contents_count();
302}
303
nick9f34e2892016-01-12 21:01:07304void SiteInstanceImpl::AddObserver(Observer* observer) {
305 observers_.AddObserver(observer);
306}
307
308void SiteInstanceImpl::RemoveObserver(Observer* observer) {
309 observers_.RemoveObserver(observer);
310}
311
[email protected]4c3a23582012-08-18 08:54:34312BrowserContext* SiteInstanceImpl::GetBrowserContext() const {
[email protected]72daaa92012-01-18 13:39:02313 return browsing_instance_->browser_context();
314}
315
nickcc0d9142015-10-14 16:27:10316// static
dchengbccd6b82016-03-30 16:24:19317scoped_refptr<SiteInstance> SiteInstance::Create(
318 BrowserContext* browser_context) {
Lukasz Anforowicz4726a172018-10-15 21:25:10319 DCHECK(browser_context);
dchengbccd6b82016-03-30 16:24:19320 return SiteInstanceImpl::Create(browser_context);
initial.commit09911bf2008-07-26 23:55:29321}
322
nickcc0d9142015-10-14 16:27:10323// static
dchengbccd6b82016-03-30 16:24:19324scoped_refptr<SiteInstance> SiteInstance::CreateForURL(
325 BrowserContext* browser_context,
326 const GURL& url) {
Lukasz Anforowicz4726a172018-10-15 21:25:10327 DCHECK(browser_context);
dchengbccd6b82016-03-30 16:24:19328 return SiteInstanceImpl::CreateForURL(browser_context, url);
[email protected]d8a96622009-05-07 19:21:16329}
330
nickcc0d9142015-10-14 16:27:10331// static
Nasko Oskovdeb6c7ea2017-11-30 18:18:11332bool SiteInstance::ShouldAssignSiteForURL(const GURL& url) {
333 return SiteInstanceImpl::ShouldAssignSiteForURL(url);
334}
335
336// static
[email protected]399583b2012-12-11 09:33:42337bool SiteInstance::IsSameWebSite(BrowserContext* browser_context,
[email protected]855d7d572014-08-02 11:18:17338 const GURL& real_src_url,
339 const GURL& real_dest_url) {
Alex Moshchuk25c64bb2017-12-02 02:50:11340 return SiteInstanceImpl::IsSameWebSite(browser_context, real_src_url,
341 real_dest_url, true);
342}
343
344bool SiteInstanceImpl::IsSameWebSite(BrowserContext* browser_context,
345 const GURL& real_src_url,
346 const GURL& real_dest_url,
347 bool should_compare_effective_urls) {
Lukasz Anforowicz4726a172018-10-15 21:25:10348 DCHECK(browser_context);
349
Alex Moshchuk25c64bb2017-12-02 02:50:11350 GURL src_url =
351 should_compare_effective_urls
352 ? SiteInstanceImpl::GetEffectiveURL(browser_context, real_src_url)
353 : real_src_url;
354 GURL dest_url =
355 should_compare_effective_urls
356 ? SiteInstanceImpl::GetEffectiveURL(browser_context, real_dest_url)
357 : real_dest_url;
[email protected]399583b2012-12-11 09:33:42358
359 // We infer web site boundaries based on the registered domain name of the
360 // top-level page and the scheme. We do not pay attention to the port if
361 // one is present, because pages served from different ports can still
362 // access each other if they change their document.domain variable.
363
364 // Some special URLs will match the site instance of any other URL. This is
365 // done before checking both of them for validity, since we want these URLs
366 // to have the same site instance as even an invalid one.
[email protected]855d7d572014-08-02 11:18:17367 if (IsRendererDebugURL(src_url) || IsRendererDebugURL(dest_url))
[email protected]399583b2012-12-11 09:33:42368 return true;
369
370 // If either URL is invalid, they aren't part of the same site.
[email protected]855d7d572014-08-02 11:18:17371 if (!src_url.is_valid() || !dest_url.is_valid())
[email protected]399583b2012-12-11 09:33:42372 return false;
373
[email protected]855d7d572014-08-02 11:18:17374 // If the destination url is just a blank page, we treat them as part of the
375 // same site.
376 GURL blank_page(url::kAboutBlankURL);
377 if (dest_url == blank_page)
378 return true;
379
Alex Moshchuka308c9b2018-02-08 20:58:14380 // If the source and destination URLs are equal excluding the hash, they have
381 // the same site. This matters for file URLs, where SameDomainOrHost() would
382 // otherwise return false below.
383 if (src_url.EqualsIgnoringRef(dest_url))
384 return true;
385
Daniel Cheng88186bd52017-10-20 08:14:46386 url::Origin src_origin = url::Origin::Create(src_url);
387 url::Origin dest_origin = url::Origin::Create(dest_url);
alexmos3b9ad102017-05-26 23:41:08388
[email protected]399583b2012-12-11 09:33:42389 // If the schemes differ, they aren't part of the same site.
alexmoscbf995782017-06-01 03:13:13390 if (src_origin.scheme() != dest_origin.scheme())
[email protected]399583b2012-12-11 09:33:42391 return false;
392
alexmos4bc26322017-07-01 00:57:14393 if (!net::registry_controlled_domains::SameDomainOrHost(
394 src_origin, dest_origin,
395 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) {
396 return false;
397 }
398
399 // If the sites are the same, check isolated origins. If either URL matches
Alex Moshchuk4e19b362018-09-10 21:14:36400 // an isolated origin, compare origins rather than sites. As an optimization
401 // to avoid unneeded isolated origin lookups, shortcut this check if the two
402 // origins are the same.
403 if (src_origin == dest_origin)
404 return true;
alexmos4bc26322017-07-01 00:57:14405 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
406 url::Origin src_isolated_origin;
407 url::Origin dest_isolated_origin;
408 bool src_origin_is_isolated =
409 policy->GetMatchingIsolatedOrigin(src_origin, &src_isolated_origin);
410 bool dest_origin_is_isolated =
411 policy->GetMatchingIsolatedOrigin(dest_origin, &dest_isolated_origin);
412 if (src_origin_is_isolated || dest_origin_is_isolated) {
413 // Compare most specific matching origins to ensure that a subdomain of an
414 // isolated origin (e.g., https://subdomain.isolated.foo.com) also matches
415 // the isolated origin's site URL (e.g., https://isolated.foo.com).
416 return src_isolated_origin == dest_isolated_origin;
417 }
418
419 return true;
[email protected]399583b2012-12-11 09:33:42420}
421
nickcc0d9142015-10-14 16:27:10422// static
[email protected]399583b2012-12-11 09:33:42423GURL SiteInstance::GetSiteForURL(BrowserContext* browser_context,
Alex Moshchuk5f926a52018-08-29 20:57:30424 const GURL& url) {
425 // By default, GetSiteForURL will resolve |real_url| to an effective URL
426 // before computing its site.
427 return SiteInstanceImpl::GetSiteForURL(browser_context, url,
428 true /* should_use_effective_urls */);
429}
430
431// static
432GURL SiteInstanceImpl::DetermineProcessLockURL(BrowserContext* browser_context,
433 const GURL& url) {
434 // For the process lock URL, convert |url| to a site without resolving |url|
435 // to an effective URL.
436 return SiteInstanceImpl::GetSiteForURL(browser_context, url,
437 false /* should_use_effective_urls */);
438}
439
440GURL SiteInstanceImpl::GetSiteForURL(BrowserContext* browser_context,
441 const GURL& real_url,
442 bool should_use_effective_urls) {
[email protected]25a9e8e532012-06-22 06:16:28443 // TODO(fsamuel, creis): For some reason appID is not recognized as a host.
[email protected]6eb1a11e2013-10-09 00:54:37444 if (real_url.SchemeIs(kGuestScheme))
[email protected]25a9e8e532012-06-22 06:16:28445 return real_url;
446
Alex Moshchuk5f926a52018-08-29 20:57:30447 GURL url = should_use_effective_urls
448 ? SiteInstanceImpl::GetEffectiveURL(browser_context, real_url)
449 : real_url;
Daniel Cheng88186bd52017-10-20 08:14:46450 url::Origin origin = url::Origin::Create(url);
[email protected]3a8eecb2010-04-22 23:56:30451
Lukasz Anforowicz48097c42017-12-15 00:23:38452 // If the url has a host, then determine the site. Skip file URLs to avoid a
453 // situation where site URL of file://localhost/ would mismatch Blink's origin
454 // (which ignores the hostname in this case - see https://crbug.com/776160).
455 if (!origin.host().empty() && origin.scheme() != url::kFileScheme) {
Alex Moshchuk4e19b362018-09-10 21:14:36456 GURL site_url(GetSiteForOrigin(origin));
457
458 // Isolated origins should use the full origin as their site URL. A
459 // subdomain of an isolated origin should also use that isolated origin's
460 // site URL. It is important to check |origin| (based on |url|) rather than
461 // |real_url| here, since some effective URLs (such as for NTP) need to be
462 // resolved prior to the isolated origin lookup.
463 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
464 url::Origin isolated_origin;
465 if (policy->GetMatchingIsolatedOrigin(origin, site_url, &isolated_origin))
466 return isolated_origin.GetURL();
Alex Moshchuk5f926a52018-08-29 20:57:30467
468 // If an effective URL was used, augment the effective site URL with the
469 // underlying web site in the hash. This is needed to keep
470 // navigations across sites covered by one hosted app in separate
471 // SiteInstances. See https://crbug.com/791796.
472 //
473 // TODO(https://crbug.com/734722): Consider replacing this hack with
474 // a proper security principal.
475 if (should_use_effective_urls && url != real_url) {
Alex Moshchuk4e19b362018-09-10 21:14:36476 std::string non_translated_site_url(
477 GetSiteForURL(browser_context, real_url,
478 false /* should_use_effective_urls */)
479 .spec());
480 GURL::Replacements replacements;
481 replacements.SetRefStr(non_translated_site_url.c_str());
482 site_url = site_url.ReplaceComponents(replacements);
Alex Moshchuk5f926a52018-08-29 20:57:30483 }
484
Alex Moshchuk4e19b362018-09-10 21:14:36485 return site_url;
initial.commit09911bf2008-07-26 23:55:29486 }
creisf60c2cd2014-12-18 00:41:02487
488 // If there is no host but there is a scheme, return the scheme.
489 // This is useful for cases like file URLs.
Chris Palmerab5e5b52018-09-28 19:19:30490 if (!origin.opaque()) {
Lukasz Anforowicz217fd272018-03-07 21:41:43491 // Prefer to use the scheme of |origin| rather than |url|, to correctly
Alex Moshchukb1f87482018-07-19 01:51:51492 // cover blob:file: and filesystem:file: URIs (see also
493 // https://crbug.com/697111).
Lukasz Anforowiczd926a842018-03-09 01:50:45494 DCHECK(!origin.scheme().empty());
495 return GURL(origin.scheme() + ":");
496 } else if (url.has_scheme()) {
Alex Moshchukb1f87482018-07-19 01:51:51497 // In some cases, it is not safe to use just the scheme as a site URL, as
Charlie Reis0bb3f5c72018-08-06 22:46:01498 // that might allow two URLs created by different sites to share a process.
499 // See https://crbug.com/863623 and https://crbug.com/863069.
Alex Moshchukb1f87482018-07-19 01:51:51500 //
501 // TODO(alexmos,creis): This should eventually be expanded to certain other
Charlie Reis0bb3f5c72018-08-06 22:46:01502 // schemes, such as file:.
503 // TODO(creis): This currently causes problems with tests on Android and
504 // Android WebView. For now, skip it when Site Isolation is not enabled,
505 // since there's no need to isolate data and blob URLs from each other in
506 // that case.
507 bool is_site_isolation_enabled =
508 SiteIsolationPolicy::UseDedicatedProcessesForAllSites() ||
509 SiteIsolationPolicy::AreIsolatedOriginsEnabled();
510 if (is_site_isolation_enabled &&
511 (url.SchemeIsBlob() || url.scheme() == url::kDataScheme)) {
Alex Moshchukb1f87482018-07-19 01:51:51512 // We get here for blob URLs of form blob:null/guid. Use the full URL
513 // with the guid in that case, which isolates all blob URLs with unique
Charlie Reis0bb3f5c72018-08-06 22:46:01514 // origins from each other. We also get here for browser-initiated
515 // navigations to data URLs, which have a unique origin and should only
516 // share a process when they are identical. Remove hash from the URL in
517 // either case, since same-document navigations shouldn't use a different
518 // site URL.
Alex Moshchukb1f87482018-07-19 01:51:51519 if (url.has_ref()) {
520 GURL::Replacements replacements;
521 replacements.ClearRef();
522 url = url.ReplaceComponents(replacements);
523 }
524 return url;
525 }
526
Lukasz Anforowiczd926a842018-03-09 01:50:45527 DCHECK(!url.scheme().empty());
creisf60c2cd2014-12-18 00:41:02528 return GURL(url.scheme() + ":");
Lukasz Anforowiczd926a842018-03-09 01:50:45529 }
creisf60c2cd2014-12-18 00:41:02530
531 // Otherwise the URL should be invalid; return an empty site.
Lukasz Anforowicz217fd272018-03-07 21:41:43532 DCHECK(!url.is_valid()) << url;
creisf60c2cd2014-12-18 00:41:02533 return GURL();
initial.commit09911bf2008-07-26 23:55:29534}
535
nickcc0d9142015-10-14 16:27:10536// static
Alex Moshchuk4e19b362018-09-10 21:14:36537GURL SiteInstanceImpl::GetSiteForOrigin(const url::Origin& origin) {
538 // Only keep the scheme and registered domain of |origin|.
539 std::string domain = net::registry_controlled_domains::GetDomainAndRegistry(
540 origin.host(),
541 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
542 std::string site = origin.scheme();
543 site += url::kStandardSchemeSeparator;
544 site += domain.empty() ? origin.host() : domain;
545 return GURL(site);
546}
547
548// static
[email protected]4c3a23582012-08-18 08:54:34549GURL SiteInstanceImpl::GetEffectiveURL(BrowserContext* browser_context,
550 const GURL& url) {
Alex Moshchuka31c7882018-01-17 00:57:30551 return GetContentClient()->browser()->GetEffectiveURL(browser_context, url);
[email protected]3a8eecb2010-04-22 23:56:30552}
553
nickcc0d9142015-10-14 16:27:10554// static
Alex Moshchuk25c64bb2017-12-02 02:50:11555bool SiteInstanceImpl::HasEffectiveURL(BrowserContext* browser_context,
556 const GURL& url) {
557 return GetEffectiveURL(browser_context, url) != url;
558}
559
560// static
nickcc0d9142015-10-14 16:27:10561bool SiteInstanceImpl::DoesSiteRequireDedicatedProcess(
562 BrowserContext* browser_context,
nickdb193a12016-09-09 23:09:23563 const GURL& url) {
Lukasz Anforowicz4726a172018-10-15 21:25:10564 DCHECK(browser_context);
565
nickcc0d9142015-10-14 16:27:10566 // If --site-per-process is enabled, site isolation is enabled everywhere.
567 if (SiteIsolationPolicy::UseDedicatedProcessesForAllSites())
568 return true;
569
alexmos3b9ad102017-05-26 23:41:08570 // Always require a dedicated process for isolated origins.
Alex Moshchuk5f926a52018-08-29 20:57:30571 GURL site_url = SiteInstance::GetSiteForURL(browser_context, url);
alexmos3b9ad102017-05-26 23:41:08572 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
Daniel Cheng88186bd52017-10-20 08:14:46573 if (policy->IsIsolatedOrigin(url::Origin::Create(site_url)))
alexmos3b9ad102017-05-26 23:41:08574 return true;
575
Lukasz Anforowicz1f5ad402018-09-28 23:57:54576 // Error pages in main frames do require isolation, however since this is
577 // missing the context whether this is for a main frame or not, that part
578 // is enforced in RenderFrameHostManager.
579 if (site_url.SchemeIs(kChromeErrorScheme))
580 return true;
581
582 // Isolate kChromeUIScheme pages from one another and from other kinds of
583 // schemes.
584 if (site_url.SchemeIs(content::kChromeUIScheme))
585 return true;
586
nickdb193a12016-09-09 23:09:23587 // Let the content embedder enable site isolation for specific URLs. Use the
588 // canonical site url for this check, so that schemes with nested origins
589 // (blob and filesystem) work properly.
Avi Drissman8c36df72017-12-11 21:19:20590 if (GetContentClient()->browser()->DoesSiteRequireDedicatedProcess(
nickdb193a12016-09-09 23:09:23591 browser_context, site_url)) {
nickcc0d9142015-10-14 16:27:10592 return true;
593 }
594
595 return false;
596}
597
alexmos13fe1962017-06-28 04:25:12598// static
599bool SiteInstanceImpl::ShouldLockToOrigin(BrowserContext* browser_context,
600 GURL site_url) {
Lukasz Anforowicz4726a172018-10-15 21:25:10601 DCHECK(browser_context);
602
creis0b49fa12017-07-10 16:31:11603 // Don't lock to origin in --single-process mode, since this mode puts
604 // cross-site pages into the same process.
Alex Moshchukd4c4ca32018-08-15 20:36:51605 if (RenderProcessHost::run_renderer_in_process())
creis0b49fa12017-07-10 16:31:11606 return false;
607
alexmos13fe1962017-06-28 04:25:12608 if (!DoesSiteRequireDedicatedProcess(browser_context, site_url))
609 return false;
610
611 // Guest processes cannot be locked to their site because guests always have
612 // a fixed SiteInstance. The site of GURLs a guest loads doesn't match that
613 // SiteInstance. So we skip locking the guest process to the site.
614 // TODO(ncarter): Remove this exclusion once we can make origin lock per
615 // RenderFrame routing id.
616 if (site_url.SchemeIs(content::kGuestScheme))
617 return false;
618
alexmos13fe1962017-06-28 04:25:12619 // TODO(creis, nick): Until we can handle sites with effective URLs at the
620 // call sites of ChildProcessSecurityPolicy::CanAccessDataForOrigin, we
621 // must give the embedder a chance to exempt some sites to avoid process
622 // kills.
623 if (!GetContentClient()->browser()->ShouldLockToOrigin(browser_context,
624 site_url)) {
625 return false;
626 }
627
628 return true;
629}
630
[email protected]40bfaf32013-11-19 01:35:43631void SiteInstanceImpl::RenderProcessHostDestroyed(RenderProcessHost* host) {
632 DCHECK_EQ(process_, host);
633 process_->RemoveObserver(this);
nickd5bbd0b2016-03-31 19:52:44634 process_ = nullptr;
[email protected]4566f132009-03-12 01:55:13635}
[email protected]313b80bd2011-11-23 03:49:10636
Bo Liu2a489402018-04-24 23:41:27637void SiteInstanceImpl::RenderProcessExited(
638 RenderProcessHost* host,
639 const ChildProcessTerminationInfo& info) {
ericwilligers254597b2016-10-17 10:32:31640 for (auto& observer : observers_)
641 observer.RenderProcessGone(this);
nick9f34e2892016-01-12 21:01:07642}
643
alexmos13fe1962017-06-28 04:25:12644void SiteInstanceImpl::LockToOriginIfNeeded() {
645 DCHECK(HasSite());
646
647 // From now on, this process should be considered "tainted" for future
648 // process reuse decisions:
649 // (1) If |site_| required a dedicated process, this SiteInstance's process
650 // can only host URLs for the same site.
651 // (2) Even if |site_| does not require a dedicated process, this
652 // SiteInstance's process still cannot be reused to host other sites
653 // requiring dedicated sites in the future.
654 // We can get here either when we commit a URL into a SiteInstance that does
655 // not yet have a site, or when we create a process for a SiteInstance with a
656 // preassigned site.
657 process_->SetIsUsed();
658
Alex Moshchuk47dd00f2017-10-10 17:15:23659 ChildProcessSecurityPolicyImpl* policy =
660 ChildProcessSecurityPolicyImpl::GetInstance();
Alex Moshchuk5f926a52018-08-29 20:57:30661 auto lock_state = policy->CheckOriginLock(process_->GetID(), lock_url());
Alex Moshchukd4c4ca32018-08-15 20:36:51662 if (ShouldLockToOrigin(GetBrowserContext(), site_)) {
Alex Moshchuk9cf5a45d2017-08-14 18:50:57663 // Sanity check that this won't try to assign an origin lock to a <webview>
664 // process, which can't be locked.
665 CHECK(!process_->IsForGuestsOnly());
666
Alex Moshchuk9cf5a45d2017-08-14 18:50:57667 switch (lock_state) {
Alex Moshchuk1656c672018-04-28 01:48:34668 case CheckOriginLockResult::NO_LOCK: {
Alex Moshchuk47dd00f2017-10-10 17:15:23669 // TODO(nick): When all sites are isolated, this operation provides
670 // strong protection. If only some sites are isolated, we need
671 // additional logic to prevent the non-isolated sites from requesting
672 // resources for isolated sites. https://crbug.com/509125
Nasko Oskov866f86cc2018-08-18 04:11:11673 TRACE_EVENT2("navigation", "SiteInstanceImpl::LockToOrigin", "site id",
Alex Moshchuk5f926a52018-08-29 20:57:30674 id_, "lock", lock_url().possibly_invalid_spec());
675 process_->LockToOrigin(lock_url());
Alex Moshchuk9cf5a45d2017-08-14 18:50:57676 break;
677 }
Alex Moshchuk1656c672018-04-28 01:48:34678 case CheckOriginLockResult::HAS_WRONG_LOCK:
Alex Moshchuk9cf5a45d2017-08-14 18:50:57679 // We should never attempt to reassign a different origin lock to a
680 // process.
Robert Sesek1e07e372017-12-09 01:34:42681 base::debug::SetCrashKeyString(bad_message::GetRequestedSiteURLKey(),
682 site_.spec());
683 base::debug::SetCrashKeyString(
684 bad_message::GetKilledProcessOriginLockKey(),
Alex Moshchuk4479b97c2017-10-17 23:20:32685 policy->GetOriginLock(process_->GetID()).spec());
Alex Moshchuk5f926a52018-08-29 20:57:30686 CHECK(false) << "Trying to lock a process to " << lock_url()
Alex Moshchuk54bf13042017-10-13 04:25:29687 << " but the process is already locked to "
688 << policy->GetOriginLock(process_->GetID());
Alex Moshchuk9cf5a45d2017-08-14 18:50:57689 break;
Alex Moshchuk1656c672018-04-28 01:48:34690 case CheckOriginLockResult::HAS_EQUAL_LOCK:
Alex Moshchuk9cf5a45d2017-08-14 18:50:57691 // Process already has the right origin lock assigned. This case will
692 // happen for commits to |site_| after the first one.
693 break;
694 default:
695 NOTREACHED();
696 }
Alex Moshchuk47dd00f2017-10-10 17:15:23697 } else {
698 // If the site that we've just committed doesn't require a dedicated
699 // process, make sure we aren't putting it in a process for a site that
700 // does.
Alex Moshchuk1656c672018-04-28 01:48:34701 if (lock_state != CheckOriginLockResult::NO_LOCK) {
702 base::debug::SetCrashKeyString(bad_message::GetRequestedSiteURLKey(),
703 site_.spec());
704 base::debug::SetCrashKeyString(
705 bad_message::GetKilledProcessOriginLockKey(),
706 policy->GetOriginLock(process_->GetID()).spec());
707 CHECK(false) << "Trying to commit non-isolated site " << site_
708 << " in process locked to "
709 << policy->GetOriginLock(process_->GetID());
710 }
[email protected]313b80bd2011-11-23 03:49:10711 }
712}
[email protected]46488322012-10-30 03:22:20713
714} // namespace content