blob: b8b2749a3c6114d951e500deaacb11d003867274 [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"
Alex Moshchuk8e5c1952019-01-15 03:39:5016#include "content/browser/isolation_context.h"
[email protected]f3b1a082011-11-18 00:34:3017#include "content/browser/renderer_host/render_process_host_impl.h"
[email protected]4c3a23582012-08-18 08:54:3418#include "content/browser/storage_partition_impl.h"
Aaron Colwellea6921f2019-01-29 16:50:3919#include "content/public/browser/browser_or_resource_context.h"
[email protected]87f3c082011-10-19 18:07:4420#include "content/public/browser/content_browser_client.h"
[email protected]f3b1a082011-11-18 00:34:3021#include "content/public/browser/render_process_host_factory.h"
Nick Carterbf6264a52018-04-06 02:39:3322#include "content/public/browser/site_isolation_policy.h"
[email protected]41fb79a52012-06-29 16:34:3323#include "content/public/browser/web_ui_controller_factory.h"
creis0b49fa12017-07-10 16:31:1124#include "content/public/common/content_switches.h"
[email protected]a1d29162011-10-14 17:14:0325#include "content/public/common/url_constants.h"
clamy7fced7b2017-11-16 19:52:4326#include "content/public/common/url_utils.h"
[email protected]be28b5f42012-07-20 11:31:2527#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
initial.commit09911bf2008-07-26 23:55:2928
[email protected]46488322012-10-30 03:22:2029namespace content {
[email protected]b6583592012-01-25 19:52:3330
avib7348942015-12-25 20:57:1031int32_t SiteInstanceImpl::next_site_instance_id_ = 1;
[email protected]992db4c2011-05-12 15:37:1532
Alex Moshchuk1656c672018-04-28 01:48:3433using CheckOriginLockResult =
34 ChildProcessSecurityPolicyImpl::CheckOriginLockResult;
35
[email protected]b6583592012-01-25 19:52:3336SiteInstanceImpl::SiteInstanceImpl(BrowsingInstance* browsing_instance)
[email protected]992db4c2011-05-12 15:37:1537 : id_(next_site_instance_id_++),
creiscce56cd2014-09-29 22:45:2238 active_frame_count_(0),
[email protected]992db4c2011-05-12 15:37:1539 browsing_instance_(browsing_instance),
nickd5bbd0b2016-03-31 19:52:4440 process_(nullptr),
Lukasz Anforowicz3caa8372018-06-05 17:22:0741 can_associate_with_spare_process_(true),
nickd5bbd0b2016-03-31 19:52:4442 has_site_(false),
Tsuyoshi Horoeb576e62017-06-28 06:00:4143 process_reuse_policy_(ProcessReusePolicy::DEFAULT),
44 is_for_service_worker_(false) {
[email protected]4566f132009-03-12 01:55:1345 DCHECK(browsing_instance);
[email protected]4566f132009-03-12 01:55:1346}
47
[email protected]b6583592012-01-25 19:52:3348SiteInstanceImpl::~SiteInstanceImpl() {
[email protected]46488322012-10-30 03:22:2049 GetContentClient()->browser()->SiteInstanceDeleting(this);
[email protected]056ad2a2011-07-12 02:13:5550
[email protected]40bfaf32013-11-19 01:35:4351 if (process_)
52 process_->RemoveObserver(this);
53
initial.commit09911bf2008-07-26 23:55:2954 // Now that no one is referencing us, we can safely remove ourselves from
55 // the BrowsingInstance. Any future visits to a page from this site
56 // (within the same BrowsingInstance) can safely create a new SiteInstance.
57 if (has_site_)
dchengbccd6b82016-03-30 16:24:1958 browsing_instance_->UnregisterSiteInstance(this);
59}
60
Nasko Oskovdeb6c7ea2017-11-30 18:18:1161// static
dchengbccd6b82016-03-30 16:24:1962scoped_refptr<SiteInstanceImpl> SiteInstanceImpl::Create(
63 BrowserContext* browser_context) {
Lukasz Anforowicz4726a172018-10-15 21:25:1064 DCHECK(browser_context);
kylecharda69d882017-10-04 05:49:5265 return base::WrapRefCounted(
dchengbccd6b82016-03-30 16:24:1966 new SiteInstanceImpl(new BrowsingInstance(browser_context)));
67}
68
Nasko Oskovdeb6c7ea2017-11-30 18:18:1169// static
dchengbccd6b82016-03-30 16:24:1970scoped_refptr<SiteInstanceImpl> SiteInstanceImpl::CreateForURL(
71 BrowserContext* browser_context,
72 const GURL& url) {
Lukasz Anforowicz4726a172018-10-15 21:25:1073 DCHECK(browser_context);
dchengbccd6b82016-03-30 16:24:1974 // This will create a new SiteInstance and BrowsingInstance.
75 scoped_refptr<BrowsingInstance> instance(
76 new BrowsingInstance(browser_context));
77 return instance->GetSiteInstanceForURL(url);
initial.commit09911bf2008-07-26 23:55:2978}
79
Nasko Oskovdeb6c7ea2017-11-30 18:18:1180// static
81bool SiteInstanceImpl::ShouldAssignSiteForURL(const GURL& url) {
82 // about:blank should not "use up" a new SiteInstance. The SiteInstance can
83 // still be used for a normal web site.
84 if (url == url::kAboutBlankURL)
85 return false;
86
87 // The embedder will then have the opportunity to determine if the URL
88 // should "use up" the SiteInstance.
89 return GetContentClient()->browser()->ShouldAssignSiteForURL(url);
90}
91
Nicolas Pena7c7847f2018-05-30 01:36:0592// static
93bool SiteInstanceImpl::IsOriginLockASite(const GURL& lock_url) {
94 return lock_url.has_scheme() && lock_url.has_host();
95}
96
avib7348942015-12-25 20:57:1097int32_t SiteInstanceImpl::GetId() {
[email protected]b6583592012-01-25 19:52:3398 return id_;
99}
100
Alex Moshchuk8e5c1952019-01-15 03:39:50101const IsolationContext& SiteInstanceImpl::GetIsolationContext() {
102 return browsing_instance_->isolation_context();
103}
104
105// static
106BrowsingInstanceId SiteInstanceImpl::NextBrowsingInstanceId() {
107 return BrowsingInstance::NextBrowsingInstanceId();
108}
109
110bool SiteInstanceImpl::HasProcess() {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28111 if (process_ != nullptr)
[email protected]41fb79a52012-06-29 16:34:33112 return true;
113
114 // If we would use process-per-site for this site, also check if there is an
115 // existing process that we would use if GetProcess() were called.
[email protected]46488322012-10-30 03:22:20116 BrowserContext* browser_context =
[email protected]41fb79a52012-06-29 16:34:33117 browsing_instance_->browser_context();
118 if (has_site_ &&
[email protected]1ae93fb12013-06-14 03:38:56119 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_) &&
Alex Moshchuk8e5c1952019-01-15 03:39:50120 RenderProcessHostImpl::GetSoleProcessHostForSite(
121 browser_context, GetIsolationContext(), site_, lock_url_)) {
[email protected]41fb79a52012-06-29 16:34:33122 return true;
123 }
124
125 return false;
[email protected]5ab79b02010-04-26 16:47:11126}
127
[email protected]41fb79a52012-06-29 16:34:33128RenderProcessHost* SiteInstanceImpl::GetProcess() {
[email protected]08c8ec7e2010-07-11 17:14:48129 // TODO(erikkay) It would be nice to ensure that the renderer type had been
130 // properly set before we get here. The default tab creation case winds up
131 // with no site set at this point, so it will default to TYPE_NORMAL. This
132 // may not be correct, so we'll wind up potentially creating a process that
133 // we then throw away, or worse sharing a process with the wrong process type.
134 // See crbug.com/43448.
135
initial.commit09911bf2008-07-26 23:55:29136 // Create a new process if ours went away or was reused.
[email protected]4566f132009-03-12 01:55:13137 if (!process_) {
[email protected]4c3a23582012-08-18 08:54:34138 BrowserContext* browser_context = browsing_instance_->browser_context();
[email protected]41fb79a52012-06-29 16:34:33139
clamy63960c32017-05-10 22:57:07140 // Check if the ProcessReusePolicy should be updated.
141 bool should_use_process_per_site =
creisa9f04ba2017-05-19 22:27:42142 has_site_ &&
143 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_);
clamy63960c32017-05-10 22:57:07144 if (should_use_process_per_site) {
145 process_reuse_policy_ = ProcessReusePolicy::PROCESS_PER_SITE;
146 } else if (process_reuse_policy_ == ProcessReusePolicy::PROCESS_PER_SITE) {
147 process_reuse_policy_ = ProcessReusePolicy::DEFAULT;
[email protected]41fb79a52012-06-29 16:34:33148 }
149
Alex Moshchuk5f926a52018-08-29 20:57:30150 process_ = RenderProcessHostImpl::GetProcessHostForSiteInstance(this);
avi6d686db2017-01-06 02:28:57151
[email protected]41fb79a52012-06-29 16:34:33152 CHECK(process_);
[email protected]40bfaf32013-11-19 01:35:43153 process_->AddObserver(this);
[email protected]41fb79a52012-06-29 16:34:33154
155 // If we are using process-per-site, we need to register this process
156 // for the current site so that we can find it again. (If no site is set
157 // at this time, we will register it in SetSite().)
clamy63960c32017-05-10 22:57:07158 if (process_reuse_policy_ == ProcessReusePolicy::PROCESS_PER_SITE &&
159 has_site_) {
Alex Moshchuk5f926a52018-08-29 20:57:30160 RenderProcessHostImpl::RegisterSoleProcessHostForSite(browser_context,
161 process_, this);
[email protected]41fb79a52012-06-29 16:34:33162 }
initial.commit09911bf2008-07-26 23:55:29163
naskob8744d22014-08-28 17:07:43164 TRACE_EVENT2("navigation", "SiteInstanceImpl::GetProcess",
165 "site id", id_, "process id", process_->GetID());
[email protected]46488322012-10-30 03:22:20166 GetContentClient()->browser()->SiteInstanceGotProcess(this);
[email protected]6f371442011-11-09 06:45:46167
[email protected]313b80bd2011-11-23 03:49:10168 if (has_site_)
alexmos13fe1962017-06-28 04:25:12169 LockToOriginIfNeeded();
initial.commit09911bf2008-07-26 23:55:29170 }
[email protected]4566f132009-03-12 01:55:13171 DCHECK(process_);
initial.commit09911bf2008-07-26 23:55:29172
[email protected]4566f132009-03-12 01:55:13173 return process_;
initial.commit09911bf2008-07-26 23:55:29174}
175
Lukasz Anforowicz3caa8372018-06-05 17:22:07176bool SiteInstanceImpl::CanAssociateWithSpareProcess() {
177 return can_associate_with_spare_process_;
178}
179
180void SiteInstanceImpl::PreventAssociationWithSpareProcess() {
181 can_associate_with_spare_process_ = false;
182}
183
[email protected]b6583592012-01-25 19:52:33184void SiteInstanceImpl::SetSite(const GURL& url) {
naskob8744d22014-08-28 17:07:43185 TRACE_EVENT2("navigation", "SiteInstanceImpl::SetSite",
186 "site id", id_, "url", url.possibly_invalid_spec());
initial.commit09911bf2008-07-26 23:55:29187 // A SiteInstance's site should not change.
188 // TODO(creis): When following links or script navigations, we can currently
189 // render pages from other sites in this SiteInstance. This will eventually
190 // be fixed, but until then, we should still not set the site of a
191 // SiteInstance more than once.
192 DCHECK(!has_site_);
193
194 // Remember that this SiteInstance has been used to load a URL, even if the
195 // URL is invalid.
196 has_site_ = true;
[email protected]4c3a23582012-08-18 08:54:34197 BrowserContext* browser_context = browsing_instance_->browser_context();
Aaron Colwellea6921f2019-01-29 16:50:39198 site_ = GetSiteForURL(BrowserOrResourceContext(browser_context),
199 GetIsolationContext(), url,
Alex Moshchuk8e5c1952019-01-15 03:39:50200 true /* should_use_effective_urls */);
Alex Moshchuk25c64bb2017-12-02 02:50:11201 original_url_ = url;
Aaron Colwellea6921f2019-01-29 16:50:39202 lock_url_ = DetermineProcessLockURL(BrowserOrResourceContext(browser_context),
203 GetIsolationContext(), url);
initial.commit09911bf2008-07-26 23:55:29204
205 // Now that we have a site, register it with the BrowsingInstance. This
206 // ensures that we won't create another SiteInstance for this site within
207 // the same BrowsingInstance, because all same-site pages within a
208 // BrowsingInstance can script each other.
209 browsing_instance_->RegisterSiteInstance(this);
[email protected]313b80bd2011-11-23 03:49:10210
clamy63960c32017-05-10 22:57:07211 // Update the process reuse policy based on the site.
212 bool should_use_process_per_site =
213 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_);
214 if (should_use_process_per_site) {
215 process_reuse_policy_ = ProcessReusePolicy::PROCESS_PER_SITE;
216 }
217
[email protected]41fb79a52012-06-29 16:34:33218 if (process_) {
alexmos13fe1962017-06-28 04:25:12219 LockToOriginIfNeeded();
[email protected]41fb79a52012-06-29 16:34:33220
221 // Ensure the process is registered for this site if necessary.
clamy63960c32017-05-10 22:57:07222 if (should_use_process_per_site) {
Alex Moshchuk5f926a52018-08-29 20:57:30223 RenderProcessHostImpl::RegisterSoleProcessHostForSite(browser_context,
224 process_, this);
[email protected]41fb79a52012-06-29 16:34:33225 }
226 }
initial.commit09911bf2008-07-26 23:55:29227}
228
[email protected]77ab17312012-09-28 15:34:59229const GURL& SiteInstanceImpl::GetSiteURL() const {
[email protected]b6583592012-01-25 19:52:33230 return site_;
231}
232
233bool SiteInstanceImpl::HasSite() const {
234 return has_site_;
235}
236
237bool SiteInstanceImpl::HasRelatedSiteInstance(const GURL& url) {
initial.commit09911bf2008-07-26 23:55:29238 return browsing_instance_->HasSiteInstance(url);
239}
240
dchengbccd6b82016-03-30 16:24:19241scoped_refptr<SiteInstance> SiteInstanceImpl::GetRelatedSiteInstance(
242 const GURL& url) {
initial.commit09911bf2008-07-26 23:55:29243 return browsing_instance_->GetSiteInstanceForURL(url);
244}
245
[email protected]14392a52012-05-02 20:28:44246bool SiteInstanceImpl::IsRelatedSiteInstance(const SiteInstance* instance) {
[email protected]fc72bb12013-06-02 21:13:46247 return browsing_instance_.get() == static_cast<const SiteInstanceImpl*>(
248 instance)->browsing_instance_.get();
[email protected]14392a52012-05-02 20:28:44249}
250
[email protected]d5072a82014-05-15 05:50:18251size_t SiteInstanceImpl::GetRelatedActiveContentsCount() {
252 return browsing_instance_->active_contents_count();
253}
254
[email protected]f88628d02012-11-11 17:58:59255bool SiteInstanceImpl::HasWrongProcessForURL(const GURL& url) {
[email protected]d292d8a2011-05-25 03:47:11256 // Having no process isn't a problem, since we'll assign it correctly.
[email protected]f88628d02012-11-11 17:58:59257 // Note that HasProcess() may return true if process_ is null, in
258 // process-per-site cases where there's an existing process available.
259 // We want to use such a process in the IsSuitableHost check, so we
260 // may end up assigning process_ in the GetProcess() call below.
261 if (!HasProcess())
[email protected]d292d8a2011-05-25 03:47:11262 return false;
263
[email protected]144a8102012-01-14 01:05:31264 // If the URL to navigate to can be associated with any site instance,
265 // we want to keep it in the same process.
[email protected]c02f1ba2014-02-03 06:53:53266 if (IsRendererDebugURL(url))
[email protected]144a8102012-01-14 01:05:31267 return false;
268
Nasko Oskov978c16b2018-08-03 22:17:06269 // Any process can host an about:blank URL, except the one used for error
270 // pages, which should not commit successful navigations. This check avoids a
271 // process transfer for browser-initiated navigations to about:blank in a
272 // dedicated process; without it, IsSuitableHost would consider this process
273 // unsuitable for about:blank when it compares origin locks.
274 // Renderer-initiated navigations will handle about:blank navigations
275 // elsewhere and leave them in the source SiteInstance, along with
276 // about:srcdoc and data:.
277 if (url.IsAboutBlank() && site_ != GURL(kUnreachableWebDataURL))
alexmos13fe1962017-06-28 04:25:12278 return false;
279
[email protected]88aae972011-12-16 01:14:18280 // If the site URL is an extension (e.g., for hosted apps or WebUI) but the
[email protected]d292d8a2011-05-25 03:47:11281 // process is not (or vice versa), make sure we notice and fix it.
Alex Moshchuk8e5c1952019-01-15 03:39:50282 GURL site_url = SiteInstanceImpl::GetSiteForURL(
283 browsing_instance_->browser_context(), GetIsolationContext(), url);
284 GURL origin_lock = DetermineProcessLockURL(
Aaron Colwellea6921f2019-01-29 16:50:39285 BrowserOrResourceContext(browsing_instance_->browser_context()),
286 GetIsolationContext(), url);
[email protected]88aae972011-12-16 01:14:18287 return !RenderProcessHostImpl::IsSuitableHost(
Alex Moshchuk8e5c1952019-01-15 03:39:50288 GetProcess(), browsing_instance_->browser_context(),
289 GetIsolationContext(), site_url, origin_lock);
[email protected]d292d8a2011-05-25 03:47:11290}
291
nickd30fd962015-07-27 21:51:08292bool SiteInstanceImpl::RequiresDedicatedProcess() {
293 if (!has_site_)
294 return false;
nickd5bbd0b2016-03-31 19:52:44295
Alex Moshchuk8e5c1952019-01-15 03:39:50296 return DoesSiteRequireDedicatedProcess(GetBrowserContext(),
297 GetIsolationContext(), site_);
nickd30fd962015-07-27 21:51:08298}
299
nick9f34e2892016-01-12 21:01:07300void SiteInstanceImpl::IncrementActiveFrameCount() {
301 active_frame_count_++;
302}
303
304void SiteInstanceImpl::DecrementActiveFrameCount() {
ericwilligers254597b2016-10-17 10:32:31305 if (--active_frame_count_ == 0) {
306 for (auto& observer : observers_)
307 observer.ActiveFrameCountIsZero(this);
308 }
nick9f34e2892016-01-12 21:01:07309}
310
[email protected]d5072a82014-05-15 05:50:18311void SiteInstanceImpl::IncrementRelatedActiveContentsCount() {
312 browsing_instance_->increment_active_contents_count();
313}
314
315void SiteInstanceImpl::DecrementRelatedActiveContentsCount() {
316 browsing_instance_->decrement_active_contents_count();
317}
318
nick9f34e2892016-01-12 21:01:07319void SiteInstanceImpl::AddObserver(Observer* observer) {
320 observers_.AddObserver(observer);
321}
322
323void SiteInstanceImpl::RemoveObserver(Observer* observer) {
324 observers_.RemoveObserver(observer);
325}
326
[email protected]4c3a23582012-08-18 08:54:34327BrowserContext* SiteInstanceImpl::GetBrowserContext() const {
[email protected]72daaa92012-01-18 13:39:02328 return browsing_instance_->browser_context();
329}
330
nickcc0d9142015-10-14 16:27:10331// static
dchengbccd6b82016-03-30 16:24:19332scoped_refptr<SiteInstance> SiteInstance::Create(
333 BrowserContext* browser_context) {
Lukasz Anforowicz4726a172018-10-15 21:25:10334 DCHECK(browser_context);
dchengbccd6b82016-03-30 16:24:19335 return SiteInstanceImpl::Create(browser_context);
initial.commit09911bf2008-07-26 23:55:29336}
337
nickcc0d9142015-10-14 16:27:10338// static
dchengbccd6b82016-03-30 16:24:19339scoped_refptr<SiteInstance> SiteInstance::CreateForURL(
340 BrowserContext* browser_context,
341 const GURL& url) {
Lukasz Anforowicz4726a172018-10-15 21:25:10342 DCHECK(browser_context);
dchengbccd6b82016-03-30 16:24:19343 return SiteInstanceImpl::CreateForURL(browser_context, url);
[email protected]d8a96622009-05-07 19:21:16344}
345
nickcc0d9142015-10-14 16:27:10346// static
Nasko Oskovdeb6c7ea2017-11-30 18:18:11347bool SiteInstance::ShouldAssignSiteForURL(const GURL& url) {
348 return SiteInstanceImpl::ShouldAssignSiteForURL(url);
349}
350
Alex Moshchuk78cf66bda2018-11-30 01:49:30351bool SiteInstanceImpl::IsSameSiteWithURL(const GURL& url) {
352 return SiteInstanceImpl::IsSameWebSite(
Alex Moshchuk8e5c1952019-01-15 03:39:50353 browsing_instance_->browser_context(), GetIsolationContext(), site_, url,
Alex Moshchuk78cf66bda2018-11-30 01:49:30354 true /* should_compare_effective_urls */);
Alex Moshchuk25c64bb2017-12-02 02:50:11355}
356
Alex Moshchuk78cf66bda2018-11-30 01:49:30357// static
Alex Moshchuk25c64bb2017-12-02 02:50:11358bool SiteInstanceImpl::IsSameWebSite(BrowserContext* browser_context,
Alex Moshchuk8e5c1952019-01-15 03:39:50359 const IsolationContext& isolation_context,
Alex Moshchuk25c64bb2017-12-02 02:50:11360 const GURL& real_src_url,
361 const GURL& real_dest_url,
362 bool should_compare_effective_urls) {
Lukasz Anforowicz4726a172018-10-15 21:25:10363 DCHECK(browser_context);
364
Alex Moshchuk25c64bb2017-12-02 02:50:11365 GURL src_url =
366 should_compare_effective_urls
367 ? SiteInstanceImpl::GetEffectiveURL(browser_context, real_src_url)
368 : real_src_url;
369 GURL dest_url =
370 should_compare_effective_urls
371 ? SiteInstanceImpl::GetEffectiveURL(browser_context, real_dest_url)
372 : real_dest_url;
[email protected]399583b2012-12-11 09:33:42373
374 // We infer web site boundaries based on the registered domain name of the
375 // top-level page and the scheme. We do not pay attention to the port if
376 // one is present, because pages served from different ports can still
377 // access each other if they change their document.domain variable.
378
379 // Some special URLs will match the site instance of any other URL. This is
380 // done before checking both of them for validity, since we want these URLs
381 // to have the same site instance as even an invalid one.
[email protected]855d7d572014-08-02 11:18:17382 if (IsRendererDebugURL(src_url) || IsRendererDebugURL(dest_url))
[email protected]399583b2012-12-11 09:33:42383 return true;
384
385 // If either URL is invalid, they aren't part of the same site.
[email protected]855d7d572014-08-02 11:18:17386 if (!src_url.is_valid() || !dest_url.is_valid())
[email protected]399583b2012-12-11 09:33:42387 return false;
388
[email protected]855d7d572014-08-02 11:18:17389 // If the destination url is just a blank page, we treat them as part of the
390 // same site.
391 GURL blank_page(url::kAboutBlankURL);
392 if (dest_url == blank_page)
393 return true;
394
Alex Moshchuka308c9b2018-02-08 20:58:14395 // If the source and destination URLs are equal excluding the hash, they have
396 // the same site. This matters for file URLs, where SameDomainOrHost() would
397 // otherwise return false below.
398 if (src_url.EqualsIgnoringRef(dest_url))
399 return true;
400
Daniel Cheng88186bd52017-10-20 08:14:46401 url::Origin src_origin = url::Origin::Create(src_url);
402 url::Origin dest_origin = url::Origin::Create(dest_url);
alexmos3b9ad102017-05-26 23:41:08403
[email protected]399583b2012-12-11 09:33:42404 // If the schemes differ, they aren't part of the same site.
alexmoscbf995782017-06-01 03:13:13405 if (src_origin.scheme() != dest_origin.scheme())
[email protected]399583b2012-12-11 09:33:42406 return false;
407
alexmos4bc26322017-07-01 00:57:14408 if (!net::registry_controlled_domains::SameDomainOrHost(
409 src_origin, dest_origin,
410 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) {
411 return false;
412 }
413
414 // If the sites are the same, check isolated origins. If either URL matches
Alex Moshchuk4e19b362018-09-10 21:14:36415 // an isolated origin, compare origins rather than sites. As an optimization
416 // to avoid unneeded isolated origin lookups, shortcut this check if the two
417 // origins are the same.
418 if (src_origin == dest_origin)
419 return true;
alexmos4bc26322017-07-01 00:57:14420 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
421 url::Origin src_isolated_origin;
422 url::Origin dest_isolated_origin;
Alex Moshchuk8e5c1952019-01-15 03:39:50423 bool src_origin_is_isolated = policy->GetMatchingIsolatedOrigin(
424 isolation_context, src_origin, &src_isolated_origin);
425 bool dest_origin_is_isolated = policy->GetMatchingIsolatedOrigin(
426 isolation_context, dest_origin, &dest_isolated_origin);
alexmos4bc26322017-07-01 00:57:14427 if (src_origin_is_isolated || dest_origin_is_isolated) {
428 // Compare most specific matching origins to ensure that a subdomain of an
429 // isolated origin (e.g., https://subdomain.isolated.foo.com) also matches
430 // the isolated origin's site URL (e.g., https://isolated.foo.com).
431 return src_isolated_origin == dest_isolated_origin;
432 }
433
434 return true;
[email protected]399583b2012-12-11 09:33:42435}
436
nickcc0d9142015-10-14 16:27:10437// static
[email protected]399583b2012-12-11 09:33:42438GURL SiteInstance::GetSiteForURL(BrowserContext* browser_context,
Alex Moshchuk5f926a52018-08-29 20:57:30439 const GURL& url) {
Aaron Colwellea6921f2019-01-29 16:50:39440 DCHECK_CURRENTLY_ON(BrowserThread::UI);
441 DCHECK(browser_context);
442
Alex Moshchuk5f926a52018-08-29 20:57:30443 // By default, GetSiteForURL will resolve |real_url| to an effective URL
Alex Moshchuk8e5c1952019-01-15 03:39:50444 // before computing its site, so set |should_use_effective_urls| to true.
445 //
446 // TODO(alexmos): Callers inside content/ should already be using the
447 // internal SiteInstanceImpl version and providing a proper IsolationContext.
448 // For callers outside content/, plumb the applicable IsolationContext here,
449 // where needed. Eventually, GetSiteForURL should always require an
450 // IsolationContext to be passed in, and this implementation should just
451 // become SiteInstanceImpl::GetSiteForURL.
Aaron Colwellea6921f2019-01-29 16:50:39452 return SiteInstanceImpl::GetSiteForURL(
453 BrowserOrResourceContext(browser_context), IsolationContext(), url,
454 true /* should_use_effective_urls */);
Alex Moshchuk5f926a52018-08-29 20:57:30455}
456
457// static
Alex Moshchuk8e5c1952019-01-15 03:39:50458GURL SiteInstanceImpl::DetermineProcessLockURL(
Aaron Colwellea6921f2019-01-29 16:50:39459 const BrowserOrResourceContext& context,
Alex Moshchuk8e5c1952019-01-15 03:39:50460 const IsolationContext& isolation_context,
461 const GURL& url) {
Alex Moshchuk5f926a52018-08-29 20:57:30462 // For the process lock URL, convert |url| to a site without resolving |url|
463 // to an effective URL.
Aaron Colwellea6921f2019-01-29 16:50:39464 return SiteInstanceImpl::GetSiteForURL(context, isolation_context, url,
Alex Moshchuk5f926a52018-08-29 20:57:30465 false /* should_use_effective_urls */);
466}
467
Aaron Colwellea6921f2019-01-29 16:50:39468// static
469GURL SiteInstanceImpl::GetSiteForURL(BrowserContext* context,
470 const IsolationContext& isolation_context,
471 const GURL& url,
472 bool should_use_effective_urls) {
473 DCHECK_CURRENTLY_ON(BrowserThread::UI);
474 return GetSiteForURL(BrowserOrResourceContext(context), isolation_context,
475 url, should_use_effective_urls);
476}
477
478GURL SiteInstanceImpl::GetSiteForURL(const BrowserOrResourceContext& context,
Alex Moshchuk8e5c1952019-01-15 03:39:50479 const IsolationContext& isolation_context,
Alex Moshchuk5f926a52018-08-29 20:57:30480 const GURL& real_url,
481 bool should_use_effective_urls) {
[email protected]25a9e8e532012-06-22 06:16:28482 // TODO(fsamuel, creis): For some reason appID is not recognized as a host.
[email protected]6eb1a11e2013-10-09 00:54:37483 if (real_url.SchemeIs(kGuestScheme))
[email protected]25a9e8e532012-06-22 06:16:28484 return real_url;
485
Aaron Colwellea6921f2019-01-29 16:50:39486 if (should_use_effective_urls)
487 DCHECK_CURRENTLY_ON(BrowserThread::UI);
488
Alex Moshchuk5f926a52018-08-29 20:57:30489 GURL url = should_use_effective_urls
Aaron Colwellea6921f2019-01-29 16:50:39490 ? SiteInstanceImpl::GetEffectiveURL(context.ToBrowserContext(),
491 real_url)
Alex Moshchuk5f926a52018-08-29 20:57:30492 : real_url;
Daniel Cheng88186bd52017-10-20 08:14:46493 url::Origin origin = url::Origin::Create(url);
[email protected]3a8eecb2010-04-22 23:56:30494
Lukasz Anforowicz48097c42017-12-15 00:23:38495 // If the url has a host, then determine the site. Skip file URLs to avoid a
496 // situation where site URL of file://localhost/ would mismatch Blink's origin
497 // (which ignores the hostname in this case - see https://crbug.com/776160).
498 if (!origin.host().empty() && origin.scheme() != url::kFileScheme) {
Alex Moshchuk4e19b362018-09-10 21:14:36499 GURL site_url(GetSiteForOrigin(origin));
500
501 // Isolated origins should use the full origin as their site URL. A
502 // subdomain of an isolated origin should also use that isolated origin's
503 // site URL. It is important to check |origin| (based on |url|) rather than
504 // |real_url| here, since some effective URLs (such as for NTP) need to be
505 // resolved prior to the isolated origin lookup.
506 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
507 url::Origin isolated_origin;
Alex Moshchuk8e5c1952019-01-15 03:39:50508 if (policy->GetMatchingIsolatedOrigin(isolation_context, origin, site_url,
509 &isolated_origin))
Alex Moshchuk4e19b362018-09-10 21:14:36510 return isolated_origin.GetURL();
Alex Moshchuk5f926a52018-08-29 20:57:30511
512 // If an effective URL was used, augment the effective site URL with the
513 // underlying web site in the hash. This is needed to keep
514 // navigations across sites covered by one hosted app in separate
515 // SiteInstances. See https://crbug.com/791796.
516 //
517 // TODO(https://crbug.com/734722): Consider replacing this hack with
518 // a proper security principal.
519 if (should_use_effective_urls && url != real_url) {
Alex Moshchuk4e19b362018-09-10 21:14:36520 std::string non_translated_site_url(
Aaron Colwellea6921f2019-01-29 16:50:39521 GetSiteForURL(context, isolation_context, real_url,
Alex Moshchuk4e19b362018-09-10 21:14:36522 false /* should_use_effective_urls */)
523 .spec());
524 GURL::Replacements replacements;
525 replacements.SetRefStr(non_translated_site_url.c_str());
526 site_url = site_url.ReplaceComponents(replacements);
Alex Moshchuk5f926a52018-08-29 20:57:30527 }
528
Alex Moshchuk4e19b362018-09-10 21:14:36529 return site_url;
initial.commit09911bf2008-07-26 23:55:29530 }
creisf60c2cd2014-12-18 00:41:02531
532 // If there is no host but there is a scheme, return the scheme.
533 // This is useful for cases like file URLs.
Chris Palmerab5e5b52018-09-28 19:19:30534 if (!origin.opaque()) {
Lukasz Anforowicz217fd272018-03-07 21:41:43535 // Prefer to use the scheme of |origin| rather than |url|, to correctly
Alex Moshchukb1f87482018-07-19 01:51:51536 // cover blob:file: and filesystem:file: URIs (see also
537 // https://crbug.com/697111).
Lukasz Anforowiczd926a842018-03-09 01:50:45538 DCHECK(!origin.scheme().empty());
539 return GURL(origin.scheme() + ":");
540 } else if (url.has_scheme()) {
Alex Moshchukb1f87482018-07-19 01:51:51541 // In some cases, it is not safe to use just the scheme as a site URL, as
Charlie Reis0bb3f5c72018-08-06 22:46:01542 // that might allow two URLs created by different sites to share a process.
543 // See https://crbug.com/863623 and https://crbug.com/863069.
Alex Moshchukb1f87482018-07-19 01:51:51544 //
545 // TODO(alexmos,creis): This should eventually be expanded to certain other
Charlie Reis0bb3f5c72018-08-06 22:46:01546 // schemes, such as file:.
547 // TODO(creis): This currently causes problems with tests on Android and
548 // Android WebView. For now, skip it when Site Isolation is not enabled,
549 // since there's no need to isolate data and blob URLs from each other in
550 // that case.
551 bool is_site_isolation_enabled =
552 SiteIsolationPolicy::UseDedicatedProcessesForAllSites() ||
553 SiteIsolationPolicy::AreIsolatedOriginsEnabled();
554 if (is_site_isolation_enabled &&
555 (url.SchemeIsBlob() || url.scheme() == url::kDataScheme)) {
Alex Moshchukb1f87482018-07-19 01:51:51556 // We get here for blob URLs of form blob:null/guid. Use the full URL
557 // with the guid in that case, which isolates all blob URLs with unique
Charlie Reis0bb3f5c72018-08-06 22:46:01558 // origins from each other. We also get here for browser-initiated
559 // navigations to data URLs, which have a unique origin and should only
560 // share a process when they are identical. Remove hash from the URL in
561 // either case, since same-document navigations shouldn't use a different
562 // site URL.
Alex Moshchukb1f87482018-07-19 01:51:51563 if (url.has_ref()) {
564 GURL::Replacements replacements;
565 replacements.ClearRef();
566 url = url.ReplaceComponents(replacements);
567 }
568 return url;
569 }
570
Lukasz Anforowiczd926a842018-03-09 01:50:45571 DCHECK(!url.scheme().empty());
creisf60c2cd2014-12-18 00:41:02572 return GURL(url.scheme() + ":");
Lukasz Anforowiczd926a842018-03-09 01:50:45573 }
creisf60c2cd2014-12-18 00:41:02574
575 // Otherwise the URL should be invalid; return an empty site.
Lukasz Anforowicz217fd272018-03-07 21:41:43576 DCHECK(!url.is_valid()) << url;
creisf60c2cd2014-12-18 00:41:02577 return GURL();
initial.commit09911bf2008-07-26 23:55:29578}
579
nickcc0d9142015-10-14 16:27:10580// static
Alex Moshchuk4e19b362018-09-10 21:14:36581GURL SiteInstanceImpl::GetSiteForOrigin(const url::Origin& origin) {
582 // Only keep the scheme and registered domain of |origin|.
583 std::string domain = net::registry_controlled_domains::GetDomainAndRegistry(
584 origin.host(),
585 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
586 std::string site = origin.scheme();
587 site += url::kStandardSchemeSeparator;
588 site += domain.empty() ? origin.host() : domain;
589 return GURL(site);
590}
591
592// static
[email protected]4c3a23582012-08-18 08:54:34593GURL SiteInstanceImpl::GetEffectiveURL(BrowserContext* browser_context,
594 const GURL& url) {
Aaron Colwellea6921f2019-01-29 16:50:39595 DCHECK(browser_context);
Alex Moshchuka31c7882018-01-17 00:57:30596 return GetContentClient()->browser()->GetEffectiveURL(browser_context, url);
[email protected]3a8eecb2010-04-22 23:56:30597}
598
nickcc0d9142015-10-14 16:27:10599// static
Alex Moshchuk25c64bb2017-12-02 02:50:11600bool SiteInstanceImpl::HasEffectiveURL(BrowserContext* browser_context,
601 const GURL& url) {
602 return GetEffectiveURL(browser_context, url) != url;
603}
604
605// static
nickcc0d9142015-10-14 16:27:10606bool SiteInstanceImpl::DoesSiteRequireDedicatedProcess(
607 BrowserContext* browser_context,
Alex Moshchuk8e5c1952019-01-15 03:39:50608 const IsolationContext& isolation_context,
nickdb193a12016-09-09 23:09:23609 const GURL& url) {
Lukasz Anforowicz4726a172018-10-15 21:25:10610 DCHECK(browser_context);
611
nickcc0d9142015-10-14 16:27:10612 // If --site-per-process is enabled, site isolation is enabled everywhere.
613 if (SiteIsolationPolicy::UseDedicatedProcessesForAllSites())
614 return true;
615
alexmos3b9ad102017-05-26 23:41:08616 // Always require a dedicated process for isolated origins.
Alex Moshchuk8e5c1952019-01-15 03:39:50617 GURL site_url =
618 SiteInstanceImpl::GetSiteForURL(browser_context, isolation_context, url,
619 true /* should_compare_effective_urls */);
alexmos3b9ad102017-05-26 23:41:08620 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
Alex Moshchuk8e5c1952019-01-15 03:39:50621 if (policy->IsIsolatedOrigin(isolation_context,
622 url::Origin::Create(site_url)))
alexmos3b9ad102017-05-26 23:41:08623 return true;
624
Lukasz Anforowicz1f5ad402018-09-28 23:57:54625 // Error pages in main frames do require isolation, however since this is
626 // missing the context whether this is for a main frame or not, that part
627 // is enforced in RenderFrameHostManager.
628 if (site_url.SchemeIs(kChromeErrorScheme))
629 return true;
630
631 // Isolate kChromeUIScheme pages from one another and from other kinds of
632 // schemes.
633 if (site_url.SchemeIs(content::kChromeUIScheme))
634 return true;
635
nickdb193a12016-09-09 23:09:23636 // Let the content embedder enable site isolation for specific URLs. Use the
637 // canonical site url for this check, so that schemes with nested origins
638 // (blob and filesystem) work properly.
Avi Drissman8c36df72017-12-11 21:19:20639 if (GetContentClient()->browser()->DoesSiteRequireDedicatedProcess(
nickdb193a12016-09-09 23:09:23640 browser_context, site_url)) {
nickcc0d9142015-10-14 16:27:10641 return true;
642 }
643
644 return false;
645}
646
alexmos13fe1962017-06-28 04:25:12647// static
Alex Moshchuk8e5c1952019-01-15 03:39:50648bool SiteInstanceImpl::ShouldLockToOrigin(
649 BrowserContext* browser_context,
650 const IsolationContext& isolation_context,
651 GURL site_url) {
Lukasz Anforowicz4726a172018-10-15 21:25:10652 DCHECK(browser_context);
653
creis0b49fa12017-07-10 16:31:11654 // Don't lock to origin in --single-process mode, since this mode puts
655 // cross-site pages into the same process.
Alex Moshchukd4c4ca32018-08-15 20:36:51656 if (RenderProcessHost::run_renderer_in_process())
creis0b49fa12017-07-10 16:31:11657 return false;
658
Alex Moshchuk8e5c1952019-01-15 03:39:50659 if (!DoesSiteRequireDedicatedProcess(browser_context, isolation_context,
660 site_url))
alexmos13fe1962017-06-28 04:25:12661 return false;
662
663 // Guest processes cannot be locked to their site because guests always have
664 // a fixed SiteInstance. The site of GURLs a guest loads doesn't match that
665 // SiteInstance. So we skip locking the guest process to the site.
666 // TODO(ncarter): Remove this exclusion once we can make origin lock per
667 // RenderFrame routing id.
668 if (site_url.SchemeIs(content::kGuestScheme))
669 return false;
670
alexmos13fe1962017-06-28 04:25:12671 // TODO(creis, nick): Until we can handle sites with effective URLs at the
672 // call sites of ChildProcessSecurityPolicy::CanAccessDataForOrigin, we
673 // must give the embedder a chance to exempt some sites to avoid process
674 // kills.
675 if (!GetContentClient()->browser()->ShouldLockToOrigin(browser_context,
676 site_url)) {
677 return false;
678 }
679
680 return true;
681}
682
Lukasz Anforowicz27851e62018-12-18 23:04:08683// static
684base::Optional<url::Origin> SiteInstanceImpl::GetRequestInitiatorSiteLock(
Lukasz Anforowicz27851e62018-12-18 23:04:08685 GURL site_url) {
Lukasz Anforowicz27851e62018-12-18 23:04:08686 // The following schemes are safe for sites that require a process lock:
687 // - data: - locking |request_initiator| to an opaque origin
688 // - http/https - requiring |request_initiator| to match |site_url| with
689 // DomainIs (i.e. suffix-based) comparison.
690 if (site_url.SchemeIsHTTPOrHTTPS() || site_url.SchemeIs(url::kDataScheme))
691 return url::Origin::Create(site_url);
692
693 // Other schemes might not be safe to use as |request_initiator_site_lock|.
694 // One example is chrome-guest://...
695 return base::nullopt;
696}
697
[email protected]40bfaf32013-11-19 01:35:43698void SiteInstanceImpl::RenderProcessHostDestroyed(RenderProcessHost* host) {
699 DCHECK_EQ(process_, host);
700 process_->RemoveObserver(this);
nickd5bbd0b2016-03-31 19:52:44701 process_ = nullptr;
[email protected]4566f132009-03-12 01:55:13702}
[email protected]313b80bd2011-11-23 03:49:10703
Bo Liu2a489402018-04-24 23:41:27704void SiteInstanceImpl::RenderProcessExited(
705 RenderProcessHost* host,
706 const ChildProcessTerminationInfo& info) {
ericwilligers254597b2016-10-17 10:32:31707 for (auto& observer : observers_)
708 observer.RenderProcessGone(this);
nick9f34e2892016-01-12 21:01:07709}
710
alexmos13fe1962017-06-28 04:25:12711void SiteInstanceImpl::LockToOriginIfNeeded() {
712 DCHECK(HasSite());
713
714 // From now on, this process should be considered "tainted" for future
715 // process reuse decisions:
716 // (1) If |site_| required a dedicated process, this SiteInstance's process
717 // can only host URLs for the same site.
718 // (2) Even if |site_| does not require a dedicated process, this
719 // SiteInstance's process still cannot be reused to host other sites
720 // requiring dedicated sites in the future.
721 // We can get here either when we commit a URL into a SiteInstance that does
722 // not yet have a site, or when we create a process for a SiteInstance with a
723 // preassigned site.
724 process_->SetIsUsed();
725
Alex Moshchuk47dd00f2017-10-10 17:15:23726 ChildProcessSecurityPolicyImpl* policy =
727 ChildProcessSecurityPolicyImpl::GetInstance();
Alex Moshchuk5f926a52018-08-29 20:57:30728 auto lock_state = policy->CheckOriginLock(process_->GetID(), lock_url());
Alex Moshchuk8e5c1952019-01-15 03:39:50729 if (ShouldLockToOrigin(GetBrowserContext(), GetIsolationContext(), site_)) {
Alex Moshchuk9cf5a45d2017-08-14 18:50:57730 // Sanity check that this won't try to assign an origin lock to a <webview>
731 // process, which can't be locked.
732 CHECK(!process_->IsForGuestsOnly());
733
Alex Moshchuk9cf5a45d2017-08-14 18:50:57734 switch (lock_state) {
Alex Moshchuk1656c672018-04-28 01:48:34735 case CheckOriginLockResult::NO_LOCK: {
Alex Moshchuk47dd00f2017-10-10 17:15:23736 // TODO(nick): When all sites are isolated, this operation provides
737 // strong protection. If only some sites are isolated, we need
738 // additional logic to prevent the non-isolated sites from requesting
739 // resources for isolated sites. https://crbug.com/509125
Nasko Oskov866f86cc2018-08-18 04:11:11740 TRACE_EVENT2("navigation", "SiteInstanceImpl::LockToOrigin", "site id",
Alex Moshchuk5f926a52018-08-29 20:57:30741 id_, "lock", lock_url().possibly_invalid_spec());
Alex Moshchuk8e5c1952019-01-15 03:39:50742 process_->LockToOrigin(GetIsolationContext(), lock_url());
Alex Moshchuk9cf5a45d2017-08-14 18:50:57743 break;
744 }
Alex Moshchuk1656c672018-04-28 01:48:34745 case CheckOriginLockResult::HAS_WRONG_LOCK:
Alex Moshchuk9cf5a45d2017-08-14 18:50:57746 // We should never attempt to reassign a different origin lock to a
747 // process.
Robert Sesek1e07e372017-12-09 01:34:42748 base::debug::SetCrashKeyString(bad_message::GetRequestedSiteURLKey(),
749 site_.spec());
750 base::debug::SetCrashKeyString(
751 bad_message::GetKilledProcessOriginLockKey(),
Alex Moshchuk4479b97c2017-10-17 23:20:32752 policy->GetOriginLock(process_->GetID()).spec());
Alex Moshchuk5f926a52018-08-29 20:57:30753 CHECK(false) << "Trying to lock a process to " << lock_url()
Alex Moshchuk54bf13042017-10-13 04:25:29754 << " but the process is already locked to "
755 << policy->GetOriginLock(process_->GetID());
Alex Moshchuk9cf5a45d2017-08-14 18:50:57756 break;
Alex Moshchuk1656c672018-04-28 01:48:34757 case CheckOriginLockResult::HAS_EQUAL_LOCK:
Alex Moshchuk9cf5a45d2017-08-14 18:50:57758 // Process already has the right origin lock assigned. This case will
759 // happen for commits to |site_| after the first one.
760 break;
761 default:
762 NOTREACHED();
763 }
Alex Moshchuk47dd00f2017-10-10 17:15:23764 } else {
765 // If the site that we've just committed doesn't require a dedicated
766 // process, make sure we aren't putting it in a process for a site that
767 // does.
Alex Moshchuk1656c672018-04-28 01:48:34768 if (lock_state != CheckOriginLockResult::NO_LOCK) {
769 base::debug::SetCrashKeyString(bad_message::GetRequestedSiteURLKey(),
770 site_.spec());
771 base::debug::SetCrashKeyString(
772 bad_message::GetKilledProcessOriginLockKey(),
773 policy->GetOriginLock(process_->GetID()).spec());
774 CHECK(false) << "Trying to commit non-isolated site " << site_
775 << " in process locked to "
776 << policy->GetOriginLock(process_->GetID());
777 }
[email protected]313b80bd2011-11-23 03:49:10778 }
779}
[email protected]46488322012-10-30 03:22:20780
781} // namespace content