blob: 9d9415fe621a8c83a92b456af8fbb340dca650b7 [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
creis0b49fa12017-07-10 16:31:117#include "base/command_line.h"
Alex Moshchuk4479b97c2017-10-17 23:20:328#include "base/debug/crash_logging.h"
alexmos3b9ad102017-05-26 23:41:089#include "base/macros.h"
avicb129c02017-05-03 06:49:2910#include "base/memory/ptr_util.h"
[email protected]39365212011-02-24 01:01:0011#include "content/browser/browsing_instance.h"
[email protected]b9535422012-02-09 01:47:5912#include "content/browser/child_process_security_policy_impl.h"
[email protected]c02f1ba2014-02-03 06:53:5313#include "content/browser/frame_host/debug_urls.h"
nick9f34e2892016-01-12 21:01:0714#include "content/browser/frame_host/frame_tree_node.h"
[email protected]f3b1a082011-11-18 00:34:3015#include "content/browser/renderer_host/render_process_host_impl.h"
[email protected]4c3a23582012-08-18 08:54:3416#include "content/browser/storage_partition_impl.h"
nickd30fd962015-07-27 21:51:0817#include "content/common/site_isolation_policy.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"
[email protected]41fb79a52012-06-29 16:34:3320#include "content/public/browser/web_ui_controller_factory.h"
creis0b49fa12017-07-10 16:31:1121#include "content/public/common/content_switches.h"
[email protected]a1d29162011-10-14 17:14:0322#include "content/public/common/url_constants.h"
clamy7fced7b2017-11-16 19:52:4323#include "content/public/common/url_utils.h"
[email protected]be28b5f42012-07-20 11:31:2524#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
initial.commit09911bf2008-07-26 23:55:2925
[email protected]46488322012-10-30 03:22:2026namespace content {
[email protected]b6583592012-01-25 19:52:3327
avib7348942015-12-25 20:57:1028int32_t SiteInstanceImpl::next_site_instance_id_ = 1;
[email protected]992db4c2011-05-12 15:37:1529
[email protected]b6583592012-01-25 19:52:3330SiteInstanceImpl::SiteInstanceImpl(BrowsingInstance* browsing_instance)
[email protected]992db4c2011-05-12 15:37:1531 : id_(next_site_instance_id_++),
creiscce56cd2014-09-29 22:45:2232 active_frame_count_(0),
[email protected]992db4c2011-05-12 15:37:1533 browsing_instance_(browsing_instance),
nickd5bbd0b2016-03-31 19:52:4434 process_(nullptr),
35 has_site_(false),
Tsuyoshi Horoeb576e62017-06-28 06:00:4136 process_reuse_policy_(ProcessReusePolicy::DEFAULT),
37 is_for_service_worker_(false) {
[email protected]4566f132009-03-12 01:55:1338 DCHECK(browsing_instance);
[email protected]4566f132009-03-12 01:55:1339}
40
[email protected]b6583592012-01-25 19:52:3341SiteInstanceImpl::~SiteInstanceImpl() {
[email protected]46488322012-10-30 03:22:2042 GetContentClient()->browser()->SiteInstanceDeleting(this);
[email protected]056ad2a2011-07-12 02:13:5543
[email protected]40bfaf32013-11-19 01:35:4344 if (process_)
45 process_->RemoveObserver(this);
46
initial.commit09911bf2008-07-26 23:55:2947 // Now that no one is referencing us, we can safely remove ourselves from
48 // the BrowsingInstance. Any future visits to a page from this site
49 // (within the same BrowsingInstance) can safely create a new SiteInstance.
50 if (has_site_)
dchengbccd6b82016-03-30 16:24:1951 browsing_instance_->UnregisterSiteInstance(this);
52}
53
Nasko Oskovdeb6c7ea2017-11-30 18:18:1154// static
dchengbccd6b82016-03-30 16:24:1955scoped_refptr<SiteInstanceImpl> SiteInstanceImpl::Create(
56 BrowserContext* browser_context) {
kylecharda69d882017-10-04 05:49:5257 return base::WrapRefCounted(
dchengbccd6b82016-03-30 16:24:1958 new SiteInstanceImpl(new BrowsingInstance(browser_context)));
59}
60
Nasko Oskovdeb6c7ea2017-11-30 18:18:1161// static
dchengbccd6b82016-03-30 16:24:1962scoped_refptr<SiteInstanceImpl> SiteInstanceImpl::CreateForURL(
63 BrowserContext* browser_context,
64 const GURL& url) {
65 // This will create a new SiteInstance and BrowsingInstance.
66 scoped_refptr<BrowsingInstance> instance(
67 new BrowsingInstance(browser_context));
68 return instance->GetSiteInstanceForURL(url);
initial.commit09911bf2008-07-26 23:55:2969}
70
Nasko Oskovdeb6c7ea2017-11-30 18:18:1171// static
72bool SiteInstanceImpl::ShouldAssignSiteForURL(const GURL& url) {
73 // about:blank should not "use up" a new SiteInstance. The SiteInstance can
74 // still be used for a normal web site.
75 if (url == url::kAboutBlankURL)
76 return false;
77
78 // The embedder will then have the opportunity to determine if the URL
79 // should "use up" the SiteInstance.
80 return GetContentClient()->browser()->ShouldAssignSiteForURL(url);
81}
82
avib7348942015-12-25 20:57:1083int32_t SiteInstanceImpl::GetId() {
[email protected]b6583592012-01-25 19:52:3384 return id_;
85}
86
87bool SiteInstanceImpl::HasProcess() const {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:2888 if (process_ != nullptr)
[email protected]41fb79a52012-06-29 16:34:3389 return true;
90
91 // If we would use process-per-site for this site, also check if there is an
92 // existing process that we would use if GetProcess() were called.
[email protected]46488322012-10-30 03:22:2093 BrowserContext* browser_context =
[email protected]41fb79a52012-06-29 16:34:3394 browsing_instance_->browser_context();
95 if (has_site_ &&
[email protected]1ae93fb12013-06-14 03:38:5696 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_) &&
[email protected]41fb79a52012-06-29 16:34:3397 RenderProcessHostImpl::GetProcessHostForSite(browser_context, site_)) {
98 return true;
99 }
100
101 return false;
[email protected]5ab79b02010-04-26 16:47:11102}
103
[email protected]41fb79a52012-06-29 16:34:33104RenderProcessHost* SiteInstanceImpl::GetProcess() {
[email protected]08c8ec7e2010-07-11 17:14:48105 // TODO(erikkay) It would be nice to ensure that the renderer type had been
106 // properly set before we get here. The default tab creation case winds up
107 // with no site set at this point, so it will default to TYPE_NORMAL. This
108 // may not be correct, so we'll wind up potentially creating a process that
109 // we then throw away, or worse sharing a process with the wrong process type.
110 // See crbug.com/43448.
111
initial.commit09911bf2008-07-26 23:55:29112 // Create a new process if ours went away or was reused.
[email protected]4566f132009-03-12 01:55:13113 if (!process_) {
[email protected]4c3a23582012-08-18 08:54:34114 BrowserContext* browser_context = browsing_instance_->browser_context();
[email protected]41fb79a52012-06-29 16:34:33115
clamy63960c32017-05-10 22:57:07116 // Check if the ProcessReusePolicy should be updated.
117 bool should_use_process_per_site =
creisa9f04ba2017-05-19 22:27:42118 has_site_ &&
119 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_);
clamy63960c32017-05-10 22:57:07120 if (should_use_process_per_site) {
121 process_reuse_policy_ = ProcessReusePolicy::PROCESS_PER_SITE;
122 } else if (process_reuse_policy_ == ProcessReusePolicy::PROCESS_PER_SITE) {
123 process_reuse_policy_ = ProcessReusePolicy::DEFAULT;
[email protected]41fb79a52012-06-29 16:34:33124 }
125
clamy63960c32017-05-10 22:57:07126 process_ = RenderProcessHostImpl::GetProcessHostForSiteInstance(
127 browser_context, this);
avi6d686db2017-01-06 02:28:57128
[email protected]41fb79a52012-06-29 16:34:33129 CHECK(process_);
[email protected]40bfaf32013-11-19 01:35:43130 process_->AddObserver(this);
[email protected]41fb79a52012-06-29 16:34:33131
132 // If we are using process-per-site, we need to register this process
133 // for the current site so that we can find it again. (If no site is set
134 // at this time, we will register it in SetSite().)
clamy63960c32017-05-10 22:57:07135 if (process_reuse_policy_ == ProcessReusePolicy::PROCESS_PER_SITE &&
136 has_site_) {
[email protected]41fb79a52012-06-29 16:34:33137 RenderProcessHostImpl::RegisterProcessHostForSite(browser_context,
138 process_, site_);
139 }
initial.commit09911bf2008-07-26 23:55:29140
naskob8744d22014-08-28 17:07:43141 TRACE_EVENT2("navigation", "SiteInstanceImpl::GetProcess",
142 "site id", id_, "process id", process_->GetID());
[email protected]46488322012-10-30 03:22:20143 GetContentClient()->browser()->SiteInstanceGotProcess(this);
[email protected]6f371442011-11-09 06:45:46144
[email protected]313b80bd2011-11-23 03:49:10145 if (has_site_)
alexmos13fe1962017-06-28 04:25:12146 LockToOriginIfNeeded();
initial.commit09911bf2008-07-26 23:55:29147 }
[email protected]4566f132009-03-12 01:55:13148 DCHECK(process_);
initial.commit09911bf2008-07-26 23:55:29149
[email protected]4566f132009-03-12 01:55:13150 return process_;
initial.commit09911bf2008-07-26 23:55:29151}
152
[email protected]b6583592012-01-25 19:52:33153void SiteInstanceImpl::SetSite(const GURL& url) {
naskob8744d22014-08-28 17:07:43154 TRACE_EVENT2("navigation", "SiteInstanceImpl::SetSite",
155 "site id", id_, "url", url.possibly_invalid_spec());
initial.commit09911bf2008-07-26 23:55:29156 // A SiteInstance's site should not change.
157 // TODO(creis): When following links or script navigations, we can currently
158 // render pages from other sites in this SiteInstance. This will eventually
159 // be fixed, but until then, we should still not set the site of a
160 // SiteInstance more than once.
161 DCHECK(!has_site_);
162
163 // Remember that this SiteInstance has been used to load a URL, even if the
164 // URL is invalid.
165 has_site_ = true;
[email protected]4c3a23582012-08-18 08:54:34166 BrowserContext* browser_context = browsing_instance_->browser_context();
[email protected]41fb79a52012-06-29 16:34:33167 site_ = GetSiteForURL(browser_context, url);
initial.commit09911bf2008-07-26 23:55:29168
169 // Now that we have a site, register it with the BrowsingInstance. This
170 // ensures that we won't create another SiteInstance for this site within
171 // the same BrowsingInstance, because all same-site pages within a
172 // BrowsingInstance can script each other.
173 browsing_instance_->RegisterSiteInstance(this);
[email protected]313b80bd2011-11-23 03:49:10174
clamy63960c32017-05-10 22:57:07175 // Update the process reuse policy based on the site.
176 bool should_use_process_per_site =
177 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_);
178 if (should_use_process_per_site) {
179 process_reuse_policy_ = ProcessReusePolicy::PROCESS_PER_SITE;
180 }
181
[email protected]41fb79a52012-06-29 16:34:33182 if (process_) {
alexmos13fe1962017-06-28 04:25:12183 LockToOriginIfNeeded();
[email protected]41fb79a52012-06-29 16:34:33184
185 // Ensure the process is registered for this site if necessary.
clamy63960c32017-05-10 22:57:07186 if (should_use_process_per_site) {
[email protected]41fb79a52012-06-29 16:34:33187 RenderProcessHostImpl::RegisterProcessHostForSite(
188 browser_context, process_, site_);
189 }
190 }
initial.commit09911bf2008-07-26 23:55:29191}
192
[email protected]77ab17312012-09-28 15:34:59193const GURL& SiteInstanceImpl::GetSiteURL() const {
[email protected]b6583592012-01-25 19:52:33194 return site_;
195}
196
197bool SiteInstanceImpl::HasSite() const {
198 return has_site_;
199}
200
201bool SiteInstanceImpl::HasRelatedSiteInstance(const GURL& url) {
initial.commit09911bf2008-07-26 23:55:29202 return browsing_instance_->HasSiteInstance(url);
203}
204
dchengbccd6b82016-03-30 16:24:19205scoped_refptr<SiteInstance> SiteInstanceImpl::GetRelatedSiteInstance(
206 const GURL& url) {
initial.commit09911bf2008-07-26 23:55:29207 return browsing_instance_->GetSiteInstanceForURL(url);
208}
209
[email protected]14392a52012-05-02 20:28:44210bool SiteInstanceImpl::IsRelatedSiteInstance(const SiteInstance* instance) {
[email protected]fc72bb12013-06-02 21:13:46211 return browsing_instance_.get() == static_cast<const SiteInstanceImpl*>(
212 instance)->browsing_instance_.get();
[email protected]14392a52012-05-02 20:28:44213}
214
[email protected]d5072a82014-05-15 05:50:18215size_t SiteInstanceImpl::GetRelatedActiveContentsCount() {
216 return browsing_instance_->active_contents_count();
217}
218
[email protected]f88628d02012-11-11 17:58:59219bool SiteInstanceImpl::HasWrongProcessForURL(const GURL& url) {
[email protected]d292d8a2011-05-25 03:47:11220 // Having no process isn't a problem, since we'll assign it correctly.
[email protected]f88628d02012-11-11 17:58:59221 // Note that HasProcess() may return true if process_ is null, in
222 // process-per-site cases where there's an existing process available.
223 // We want to use such a process in the IsSuitableHost check, so we
224 // may end up assigning process_ in the GetProcess() call below.
225 if (!HasProcess())
[email protected]d292d8a2011-05-25 03:47:11226 return false;
227
[email protected]144a8102012-01-14 01:05:31228 // If the URL to navigate to can be associated with any site instance,
229 // we want to keep it in the same process.
[email protected]c02f1ba2014-02-03 06:53:53230 if (IsRendererDebugURL(url))
[email protected]144a8102012-01-14 01:05:31231 return false;
232
alexmos13fe1962017-06-28 04:25:12233 // Any process can host an about:blank URL. This check avoids a process
234 // transfer for browser-initiated navigations to about:blank in a dedicated
235 // process; without it, IsSuitableHost would consider this process unsuitable
236 // for about:blank when it compares origin locks. Renderer-initiated
237 // navigations will handle about:blank navigations elsewhere and leave them
238 // in the source SiteInstance, along with about:srcdoc and data:.
239 if (url == url::kAboutBlankURL)
240 return false;
241
[email protected]88aae972011-12-16 01:14:18242 // If the site URL is an extension (e.g., for hosted apps or WebUI) but the
[email protected]d292d8a2011-05-25 03:47:11243 // process is not (or vice versa), make sure we notice and fix it.
[email protected]2a5221b2011-09-27 23:07:31244 GURL site_url = GetSiteForURL(browsing_instance_->browser_context(), url);
[email protected]88aae972011-12-16 01:14:18245 return !RenderProcessHostImpl::IsSuitableHost(
[email protected]f88628d02012-11-11 17:58:59246 GetProcess(), browsing_instance_->browser_context(), site_url);
[email protected]d292d8a2011-05-25 03:47:11247}
248
nickd5bbd0b2016-03-31 19:52:44249scoped_refptr<SiteInstanceImpl>
250SiteInstanceImpl::GetDefaultSubframeSiteInstance() {
251 return browsing_instance_->GetDefaultSubframeSiteInstance();
252}
253
nickd30fd962015-07-27 21:51:08254bool SiteInstanceImpl::RequiresDedicatedProcess() {
255 if (!has_site_)
256 return false;
nickd5bbd0b2016-03-31 19:52:44257
258 return DoesSiteRequireDedicatedProcess(GetBrowserContext(), site_);
nickd30fd962015-07-27 21:51:08259}
260
avi85cacb72016-10-26 22:39:33261bool SiteInstanceImpl::IsDefaultSubframeSiteInstance() const {
clamy63960c32017-05-10 22:57:07262 return process_reuse_policy_ ==
263 ProcessReusePolicy::USE_DEFAULT_SUBFRAME_PROCESS;
avi85cacb72016-10-26 22:39:33264}
265
nick9f34e2892016-01-12 21:01:07266void SiteInstanceImpl::IncrementActiveFrameCount() {
267 active_frame_count_++;
268}
269
270void SiteInstanceImpl::DecrementActiveFrameCount() {
ericwilligers254597b2016-10-17 10:32:31271 if (--active_frame_count_ == 0) {
272 for (auto& observer : observers_)
273 observer.ActiveFrameCountIsZero(this);
274 }
nick9f34e2892016-01-12 21:01:07275}
276
[email protected]d5072a82014-05-15 05:50:18277void SiteInstanceImpl::IncrementRelatedActiveContentsCount() {
278 browsing_instance_->increment_active_contents_count();
279}
280
281void SiteInstanceImpl::DecrementRelatedActiveContentsCount() {
282 browsing_instance_->decrement_active_contents_count();
283}
284
nick9f34e2892016-01-12 21:01:07285void SiteInstanceImpl::AddObserver(Observer* observer) {
286 observers_.AddObserver(observer);
287}
288
289void SiteInstanceImpl::RemoveObserver(Observer* observer) {
290 observers_.RemoveObserver(observer);
291}
292
[email protected]4c3a23582012-08-18 08:54:34293BrowserContext* SiteInstanceImpl::GetBrowserContext() const {
[email protected]72daaa92012-01-18 13:39:02294 return browsing_instance_->browser_context();
295}
296
nickcc0d9142015-10-14 16:27:10297// static
dchengbccd6b82016-03-30 16:24:19298scoped_refptr<SiteInstance> SiteInstance::Create(
299 BrowserContext* browser_context) {
300 return SiteInstanceImpl::Create(browser_context);
initial.commit09911bf2008-07-26 23:55:29301}
302
nickcc0d9142015-10-14 16:27:10303// static
dchengbccd6b82016-03-30 16:24:19304scoped_refptr<SiteInstance> SiteInstance::CreateForURL(
305 BrowserContext* browser_context,
306 const GURL& url) {
307 return SiteInstanceImpl::CreateForURL(browser_context, url);
[email protected]d8a96622009-05-07 19:21:16308}
309
nickcc0d9142015-10-14 16:27:10310// static
Nasko Oskovdeb6c7ea2017-11-30 18:18:11311bool SiteInstance::ShouldAssignSiteForURL(const GURL& url) {
312 return SiteInstanceImpl::ShouldAssignSiteForURL(url);
313}
314
315// static
[email protected]399583b2012-12-11 09:33:42316bool SiteInstance::IsSameWebSite(BrowserContext* browser_context,
[email protected]855d7d572014-08-02 11:18:17317 const GURL& real_src_url,
318 const GURL& real_dest_url) {
319 GURL src_url = SiteInstanceImpl::GetEffectiveURL(browser_context,
320 real_src_url);
321 GURL dest_url = SiteInstanceImpl::GetEffectiveURL(browser_context,
322 real_dest_url);
[email protected]399583b2012-12-11 09:33:42323
324 // We infer web site boundaries based on the registered domain name of the
325 // top-level page and the scheme. We do not pay attention to the port if
326 // one is present, because pages served from different ports can still
327 // access each other if they change their document.domain variable.
328
329 // Some special URLs will match the site instance of any other URL. This is
330 // done before checking both of them for validity, since we want these URLs
331 // to have the same site instance as even an invalid one.
[email protected]855d7d572014-08-02 11:18:17332 if (IsRendererDebugURL(src_url) || IsRendererDebugURL(dest_url))
[email protected]399583b2012-12-11 09:33:42333 return true;
334
335 // If either URL is invalid, they aren't part of the same site.
[email protected]855d7d572014-08-02 11:18:17336 if (!src_url.is_valid() || !dest_url.is_valid())
[email protected]399583b2012-12-11 09:33:42337 return false;
338
[email protected]855d7d572014-08-02 11:18:17339 // If the destination url is just a blank page, we treat them as part of the
340 // same site.
341 GURL blank_page(url::kAboutBlankURL);
342 if (dest_url == blank_page)
343 return true;
344
Daniel Cheng88186bd52017-10-20 08:14:46345 url::Origin src_origin = url::Origin::Create(src_url);
346 url::Origin dest_origin = url::Origin::Create(dest_url);
alexmos3b9ad102017-05-26 23:41:08347
[email protected]399583b2012-12-11 09:33:42348 // If the schemes differ, they aren't part of the same site.
alexmoscbf995782017-06-01 03:13:13349 if (src_origin.scheme() != dest_origin.scheme())
[email protected]399583b2012-12-11 09:33:42350 return false;
351
alexmos4bc26322017-07-01 00:57:14352 if (!net::registry_controlled_domains::SameDomainOrHost(
353 src_origin, dest_origin,
354 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) {
355 return false;
356 }
357
358 // If the sites are the same, check isolated origins. If either URL matches
359 // an isolated origin, compare origins rather than sites.
360 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
361 url::Origin src_isolated_origin;
362 url::Origin dest_isolated_origin;
363 bool src_origin_is_isolated =
364 policy->GetMatchingIsolatedOrigin(src_origin, &src_isolated_origin);
365 bool dest_origin_is_isolated =
366 policy->GetMatchingIsolatedOrigin(dest_origin, &dest_isolated_origin);
367 if (src_origin_is_isolated || dest_origin_is_isolated) {
368 // Compare most specific matching origins to ensure that a subdomain of an
369 // isolated origin (e.g., https://subdomain.isolated.foo.com) also matches
370 // the isolated origin's site URL (e.g., https://isolated.foo.com).
371 return src_isolated_origin == dest_isolated_origin;
372 }
373
374 return true;
[email protected]399583b2012-12-11 09:33:42375}
376
nickcc0d9142015-10-14 16:27:10377// static
[email protected]399583b2012-12-11 09:33:42378GURL SiteInstance::GetSiteForURL(BrowserContext* browser_context,
379 const GURL& real_url) {
[email protected]25a9e8e532012-06-22 06:16:28380 // TODO(fsamuel, creis): For some reason appID is not recognized as a host.
[email protected]6eb1a11e2013-10-09 00:54:37381 if (real_url.SchemeIs(kGuestScheme))
[email protected]25a9e8e532012-06-22 06:16:28382 return real_url;
383
[email protected]b6583592012-01-25 19:52:33384 GURL url = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url);
Daniel Cheng88186bd52017-10-20 08:14:46385 url::Origin origin = url::Origin::Create(url);
[email protected]3a8eecb2010-04-22 23:56:30386
alexmos4bc26322017-07-01 00:57:14387 // Isolated origins should use the full origin as their site URL. A subdomain
Alex Moshchukbb99a332017-11-22 04:49:57388 // of an isolated origin should also use that isolated origin's site URL. It
389 // is important to check |url| rather than |real_url| here, since some
390 // effective URLs (such as for NTP) need to be resolved prior to the isolated
391 // origin lookup.
alexmos3b9ad102017-05-26 23:41:08392 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
alexmos4bc26322017-07-01 00:57:14393 url::Origin isolated_origin;
Alex Moshchukbb99a332017-11-22 04:49:57394 if (policy->GetMatchingIsolatedOrigin(url::Origin::Create(url),
alexmos4bc26322017-07-01 00:57:14395 &isolated_origin)) {
396 return isolated_origin.GetURL();
397 }
alexmos3b9ad102017-05-26 23:41:08398
initial.commit09911bf2008-07-26 23:55:29399 // If the url has a host, then determine the site.
nick1dd47922016-04-29 16:44:48400 if (!origin.host().empty()) {
401 // Only keep the scheme and registered domain of |origin|.
402 std::string domain = net::registry_controlled_domains::GetDomainAndRegistry(
403 origin.host(),
404 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
405 std::string site = origin.scheme();
406 site += url::kStandardSchemeSeparator;
407 site += domain.empty() ? origin.host() : domain;
408 return GURL(site);
initial.commit09911bf2008-07-26 23:55:29409 }
creisf60c2cd2014-12-18 00:41:02410
411 // If there is no host but there is a scheme, return the scheme.
412 // This is useful for cases like file URLs.
413 if (url.has_scheme())
414 return GURL(url.scheme() + ":");
415
416 // Otherwise the URL should be invalid; return an empty site.
417 DCHECK(!url.is_valid());
418 return GURL();
initial.commit09911bf2008-07-26 23:55:29419}
420
nickcc0d9142015-10-14 16:27:10421// static
[email protected]4c3a23582012-08-18 08:54:34422GURL SiteInstanceImpl::GetEffectiveURL(BrowserContext* browser_context,
423 const GURL& url) {
alexmos3b9ad102017-05-26 23:41:08424 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
Alex Moshchukbb99a332017-11-22 04:49:57425 bool is_isolated_origin = policy->IsIsolatedOrigin(url::Origin::Create(url));
426 return GetContentClient()->browser()->GetEffectiveURL(browser_context, url,
427 is_isolated_origin);
[email protected]3a8eecb2010-04-22 23:56:30428}
429
nickcc0d9142015-10-14 16:27:10430// static
431bool SiteInstanceImpl::DoesSiteRequireDedicatedProcess(
432 BrowserContext* browser_context,
nickdb193a12016-09-09 23:09:23433 const GURL& url) {
nickcc0d9142015-10-14 16:27:10434 // If --site-per-process is enabled, site isolation is enabled everywhere.
435 if (SiteIsolationPolicy::UseDedicatedProcessesForAllSites())
436 return true;
437
alexmos3b9ad102017-05-26 23:41:08438 // Always require a dedicated process for isolated origins.
439 GURL site_url = GetSiteForURL(browser_context, url);
440 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
Daniel Cheng88186bd52017-10-20 08:14:46441 if (policy->IsIsolatedOrigin(url::Origin::Create(site_url)))
alexmos3b9ad102017-05-26 23:41:08442 return true;
443
nickdb193a12016-09-09 23:09:23444 // Let the content embedder enable site isolation for specific URLs. Use the
445 // canonical site url for this check, so that schemes with nested origins
446 // (blob and filesystem) work properly.
nickcc0d9142015-10-14 16:27:10447 if (GetContentClient()->IsSupplementarySiteIsolationModeEnabled() &&
448 GetContentClient()->browser()->DoesSiteRequireDedicatedProcess(
nickdb193a12016-09-09 23:09:23449 browser_context, site_url)) {
nickcc0d9142015-10-14 16:27:10450 return true;
451 }
452
453 return false;
454}
455
alexmos13fe1962017-06-28 04:25:12456// static
457bool SiteInstanceImpl::ShouldLockToOrigin(BrowserContext* browser_context,
Alex Moshchuk9cf5a45d2017-08-14 18:50:57458 RenderProcessHost* host,
alexmos13fe1962017-06-28 04:25:12459 GURL site_url) {
creis0b49fa12017-07-10 16:31:11460 // Don't lock to origin in --single-process mode, since this mode puts
461 // cross-site pages into the same process.
Alex Moshchuk9cf5a45d2017-08-14 18:50:57462 if (host->run_renderer_in_process())
creis0b49fa12017-07-10 16:31:11463 return false;
464
alexmos13fe1962017-06-28 04:25:12465 if (!DoesSiteRequireDedicatedProcess(browser_context, site_url))
466 return false;
467
468 // Guest processes cannot be locked to their site because guests always have
469 // a fixed SiteInstance. The site of GURLs a guest loads doesn't match that
470 // SiteInstance. So we skip locking the guest process to the site.
471 // TODO(ncarter): Remove this exclusion once we can make origin lock per
472 // RenderFrame routing id.
473 if (site_url.SchemeIs(content::kGuestScheme))
474 return false;
475
476 // TODO(creis, nick) https://crbug.com/510588 Chrome UI pages use the same
477 // site (chrome://chrome), so they can't be locked because the site being
478 // loaded doesn't match the SiteInstance.
479 if (site_url.SchemeIs(content::kChromeUIScheme))
480 return false;
481
482 // TODO(creis, nick): Until we can handle sites with effective URLs at the
483 // call sites of ChildProcessSecurityPolicy::CanAccessDataForOrigin, we
484 // must give the embedder a chance to exempt some sites to avoid process
485 // kills.
486 if (!GetContentClient()->browser()->ShouldLockToOrigin(browser_context,
487 site_url)) {
488 return false;
489 }
490
491 return true;
492}
493
[email protected]40bfaf32013-11-19 01:35:43494void SiteInstanceImpl::RenderProcessHostDestroyed(RenderProcessHost* host) {
495 DCHECK_EQ(process_, host);
496 process_->RemoveObserver(this);
nickd5bbd0b2016-03-31 19:52:44497 process_ = nullptr;
[email protected]4566f132009-03-12 01:55:13498}
[email protected]313b80bd2011-11-23 03:49:10499
nick9f34e2892016-01-12 21:01:07500void SiteInstanceImpl::RenderProcessWillExit(RenderProcessHost* host) {
501 // TODO(nick): http://crbug.com/575400 - RenderProcessWillExit might not serve
502 // any purpose here.
ericwilligers254597b2016-10-17 10:32:31503 for (auto& observer : observers_)
504 observer.RenderProcessGone(this);
nick9f34e2892016-01-12 21:01:07505}
506
507void SiteInstanceImpl::RenderProcessExited(RenderProcessHost* host,
508 base::TerminationStatus status,
509 int exit_code) {
ericwilligers254597b2016-10-17 10:32:31510 for (auto& observer : observers_)
511 observer.RenderProcessGone(this);
nick9f34e2892016-01-12 21:01:07512}
513
alexmos13fe1962017-06-28 04:25:12514void SiteInstanceImpl::LockToOriginIfNeeded() {
515 DCHECK(HasSite());
516
517 // From now on, this process should be considered "tainted" for future
518 // process reuse decisions:
519 // (1) If |site_| required a dedicated process, this SiteInstance's process
520 // can only host URLs for the same site.
521 // (2) Even if |site_| does not require a dedicated process, this
522 // SiteInstance's process still cannot be reused to host other sites
523 // requiring dedicated sites in the future.
524 // We can get here either when we commit a URL into a SiteInstance that does
525 // not yet have a site, or when we create a process for a SiteInstance with a
526 // preassigned site.
527 process_->SetIsUsed();
528
Alex Moshchuk47dd00f2017-10-10 17:15:23529 ChildProcessSecurityPolicyImpl* policy =
530 ChildProcessSecurityPolicyImpl::GetInstance();
531 auto lock_state = policy->CheckOriginLock(process_->GetID(), site_);
Alex Moshchuk9cf5a45d2017-08-14 18:50:57532 if (ShouldLockToOrigin(GetBrowserContext(), process_, site_)) {
Alex Moshchuk9cf5a45d2017-08-14 18:50:57533 // Sanity check that this won't try to assign an origin lock to a <webview>
534 // process, which can't be locked.
535 CHECK(!process_->IsForGuestsOnly());
536
Alex Moshchuk9cf5a45d2017-08-14 18:50:57537 switch (lock_state) {
538 case ChildProcessSecurityPolicyImpl::CheckOriginLockResult::NO_LOCK: {
Alex Moshchuk47dd00f2017-10-10 17:15:23539 // TODO(nick): When all sites are isolated, this operation provides
540 // strong protection. If only some sites are isolated, we need
541 // additional logic to prevent the non-isolated sites from requesting
542 // resources for isolated sites. https://crbug.com/509125
Alex Moshchuk9cf5a45d2017-08-14 18:50:57543 policy->LockToOrigin(process_->GetID(), site_);
544 break;
545 }
546 case ChildProcessSecurityPolicyImpl::CheckOriginLockResult::
547 HAS_WRONG_LOCK:
548 // We should never attempt to reassign a different origin lock to a
549 // process.
Alex Moshchuk4479b97c2017-10-17 23:20:32550 base::debug::SetCrashKeyValue("requested_site_url", site_.spec());
551 base::debug::SetCrashKeyValue(
552 "killed_process_origin_lock",
553 policy->GetOriginLock(process_->GetID()).spec());
Alex Moshchuk54bf13042017-10-13 04:25:29554 CHECK(false) << "Trying to lock a process to " << site_
555 << " but the process is already locked to "
556 << policy->GetOriginLock(process_->GetID());
Alex Moshchuk9cf5a45d2017-08-14 18:50:57557 break;
558 case ChildProcessSecurityPolicyImpl::CheckOriginLockResult::
559 HAS_EQUAL_LOCK:
560 // Process already has the right origin lock assigned. This case will
561 // happen for commits to |site_| after the first one.
562 break;
563 default:
564 NOTREACHED();
565 }
Alex Moshchuk47dd00f2017-10-10 17:15:23566 } else {
567 // If the site that we've just committed doesn't require a dedicated
568 // process, make sure we aren't putting it in a process for a site that
569 // does.
Alex Moshchuk4479b97c2017-10-17 23:20:32570 base::debug::SetCrashKeyValue("requested_site_url", site_.spec());
571 base::debug::SetCrashKeyValue(
572 "killed_process_origin_lock",
573 policy->GetOriginLock(process_->GetID()).spec());
Alex Moshchuk47dd00f2017-10-10 17:15:23574 CHECK_EQ(lock_state,
Alex Moshchuk54bf13042017-10-13 04:25:29575 ChildProcessSecurityPolicyImpl::CheckOriginLockResult::NO_LOCK)
576 << "Trying to commit non-isolated site " << site_
577 << " in process locked to " << policy->GetOriginLock(process_->GetID());
[email protected]313b80bd2011-11-23 03:49:10578 }
579}
[email protected]46488322012-10-30 03:22:20580
581} // namespace content