blob: 6be22050f5c59a00a3d7703a8df65b8860a103eb [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"
alexmos3b9ad102017-05-26 23:41:088#include "base/macros.h"
avicb129c02017-05-03 06:49:299#include "base/memory/ptr_util.h"
[email protected]39365212011-02-24 01:01:0010#include "content/browser/browsing_instance.h"
[email protected]b9535422012-02-09 01:47:5911#include "content/browser/child_process_security_policy_impl.h"
[email protected]c02f1ba2014-02-03 06:53:5312#include "content/browser/frame_host/debug_urls.h"
nick9f34e2892016-01-12 21:01:0713#include "content/browser/frame_host/frame_tree_node.h"
[email protected]f3b1a082011-11-18 00:34:3014#include "content/browser/renderer_host/render_process_host_impl.h"
[email protected]4c3a23582012-08-18 08:54:3415#include "content/browser/storage_partition_impl.h"
nickd30fd962015-07-27 21:51:0816#include "content/common/site_isolation_policy.h"
[email protected]87f3c082011-10-19 18:07:4417#include "content/public/browser/content_browser_client.h"
[email protected]f3b1a082011-11-18 00:34:3018#include "content/public/browser/render_process_host_factory.h"
[email protected]41fb79a52012-06-29 16:34:3319#include "content/public/browser/web_ui_controller_factory.h"
creis0b49fa12017-07-10 16:31:1120#include "content/public/common/content_switches.h"
[email protected]a1d29162011-10-14 17:14:0321#include "content/public/common/url_constants.h"
[email protected]be28b5f42012-07-20 11:31:2522#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
initial.commit09911bf2008-07-26 23:55:2923
[email protected]46488322012-10-30 03:22:2024namespace content {
[email protected]b6583592012-01-25 19:52:3325
avib7348942015-12-25 20:57:1026int32_t SiteInstanceImpl::next_site_instance_id_ = 1;
[email protected]992db4c2011-05-12 15:37:1527
[email protected]b6583592012-01-25 19:52:3328SiteInstanceImpl::SiteInstanceImpl(BrowsingInstance* browsing_instance)
[email protected]992db4c2011-05-12 15:37:1529 : id_(next_site_instance_id_++),
creiscce56cd2014-09-29 22:45:2230 active_frame_count_(0),
[email protected]992db4c2011-05-12 15:37:1531 browsing_instance_(browsing_instance),
nickd5bbd0b2016-03-31 19:52:4432 process_(nullptr),
33 has_site_(false),
Tsuyoshi Horoeb576e62017-06-28 06:00:4134 process_reuse_policy_(ProcessReusePolicy::DEFAULT),
35 is_for_service_worker_(false) {
[email protected]4566f132009-03-12 01:55:1336 DCHECK(browsing_instance);
[email protected]4566f132009-03-12 01:55:1337}
38
[email protected]b6583592012-01-25 19:52:3339SiteInstanceImpl::~SiteInstanceImpl() {
[email protected]46488322012-10-30 03:22:2040 GetContentClient()->browser()->SiteInstanceDeleting(this);
[email protected]056ad2a2011-07-12 02:13:5541
[email protected]40bfaf32013-11-19 01:35:4342 if (process_)
43 process_->RemoveObserver(this);
44
initial.commit09911bf2008-07-26 23:55:2945 // Now that no one is referencing us, we can safely remove ourselves from
46 // the BrowsingInstance. Any future visits to a page from this site
47 // (within the same BrowsingInstance) can safely create a new SiteInstance.
48 if (has_site_)
dchengbccd6b82016-03-30 16:24:1949 browsing_instance_->UnregisterSiteInstance(this);
50}
51
52scoped_refptr<SiteInstanceImpl> SiteInstanceImpl::Create(
53 BrowserContext* browser_context) {
kylecharda69d882017-10-04 05:49:5254 return base::WrapRefCounted(
dchengbccd6b82016-03-30 16:24:1955 new SiteInstanceImpl(new BrowsingInstance(browser_context)));
56}
57
58scoped_refptr<SiteInstanceImpl> SiteInstanceImpl::CreateForURL(
59 BrowserContext* browser_context,
60 const GURL& url) {
61 // This will create a new SiteInstance and BrowsingInstance.
62 scoped_refptr<BrowsingInstance> instance(
63 new BrowsingInstance(browser_context));
64 return instance->GetSiteInstanceForURL(url);
initial.commit09911bf2008-07-26 23:55:2965}
66
avib7348942015-12-25 20:57:1067int32_t SiteInstanceImpl::GetId() {
[email protected]b6583592012-01-25 19:52:3368 return id_;
69}
70
71bool SiteInstanceImpl::HasProcess() const {
[email protected]41fb79a52012-06-29 16:34:3372 if (process_ != NULL)
73 return true;
74
75 // If we would use process-per-site for this site, also check if there is an
76 // existing process that we would use if GetProcess() were called.
[email protected]46488322012-10-30 03:22:2077 BrowserContext* browser_context =
[email protected]41fb79a52012-06-29 16:34:3378 browsing_instance_->browser_context();
79 if (has_site_ &&
[email protected]1ae93fb12013-06-14 03:38:5680 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_) &&
[email protected]41fb79a52012-06-29 16:34:3381 RenderProcessHostImpl::GetProcessHostForSite(browser_context, site_)) {
82 return true;
83 }
84
85 return false;
[email protected]5ab79b02010-04-26 16:47:1186}
87
[email protected]41fb79a52012-06-29 16:34:3388RenderProcessHost* SiteInstanceImpl::GetProcess() {
[email protected]08c8ec7e2010-07-11 17:14:4889 // TODO(erikkay) It would be nice to ensure that the renderer type had been
90 // properly set before we get here. The default tab creation case winds up
91 // with no site set at this point, so it will default to TYPE_NORMAL. This
92 // may not be correct, so we'll wind up potentially creating a process that
93 // we then throw away, or worse sharing a process with the wrong process type.
94 // See crbug.com/43448.
95
initial.commit09911bf2008-07-26 23:55:2996 // Create a new process if ours went away or was reused.
[email protected]4566f132009-03-12 01:55:1397 if (!process_) {
[email protected]4c3a23582012-08-18 08:54:3498 BrowserContext* browser_context = browsing_instance_->browser_context();
[email protected]41fb79a52012-06-29 16:34:3399
clamy63960c32017-05-10 22:57:07100 // Check if the ProcessReusePolicy should be updated.
101 bool should_use_process_per_site =
creisa9f04ba2017-05-19 22:27:42102 has_site_ &&
103 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_);
clamy63960c32017-05-10 22:57:07104 if (should_use_process_per_site) {
105 process_reuse_policy_ = ProcessReusePolicy::PROCESS_PER_SITE;
106 } else if (process_reuse_policy_ == ProcessReusePolicy::PROCESS_PER_SITE) {
107 process_reuse_policy_ = ProcessReusePolicy::DEFAULT;
[email protected]41fb79a52012-06-29 16:34:33108 }
109
clamy63960c32017-05-10 22:57:07110 process_ = RenderProcessHostImpl::GetProcessHostForSiteInstance(
111 browser_context, this);
avi6d686db2017-01-06 02:28:57112
[email protected]41fb79a52012-06-29 16:34:33113 CHECK(process_);
[email protected]40bfaf32013-11-19 01:35:43114 process_->AddObserver(this);
[email protected]41fb79a52012-06-29 16:34:33115
116 // If we are using process-per-site, we need to register this process
117 // for the current site so that we can find it again. (If no site is set
118 // at this time, we will register it in SetSite().)
clamy63960c32017-05-10 22:57:07119 if (process_reuse_policy_ == ProcessReusePolicy::PROCESS_PER_SITE &&
120 has_site_) {
[email protected]41fb79a52012-06-29 16:34:33121 RenderProcessHostImpl::RegisterProcessHostForSite(browser_context,
122 process_, site_);
123 }
initial.commit09911bf2008-07-26 23:55:29124
naskob8744d22014-08-28 17:07:43125 TRACE_EVENT2("navigation", "SiteInstanceImpl::GetProcess",
126 "site id", id_, "process id", process_->GetID());
[email protected]46488322012-10-30 03:22:20127 GetContentClient()->browser()->SiteInstanceGotProcess(this);
[email protected]6f371442011-11-09 06:45:46128
[email protected]313b80bd2011-11-23 03:49:10129 if (has_site_)
alexmos13fe1962017-06-28 04:25:12130 LockToOriginIfNeeded();
initial.commit09911bf2008-07-26 23:55:29131 }
[email protected]4566f132009-03-12 01:55:13132 DCHECK(process_);
initial.commit09911bf2008-07-26 23:55:29133
[email protected]4566f132009-03-12 01:55:13134 return process_;
initial.commit09911bf2008-07-26 23:55:29135}
136
[email protected]b6583592012-01-25 19:52:33137void SiteInstanceImpl::SetSite(const GURL& url) {
naskob8744d22014-08-28 17:07:43138 TRACE_EVENT2("navigation", "SiteInstanceImpl::SetSite",
139 "site id", id_, "url", url.possibly_invalid_spec());
initial.commit09911bf2008-07-26 23:55:29140 // A SiteInstance's site should not change.
141 // TODO(creis): When following links or script navigations, we can currently
142 // render pages from other sites in this SiteInstance. This will eventually
143 // be fixed, but until then, we should still not set the site of a
144 // SiteInstance more than once.
145 DCHECK(!has_site_);
146
147 // Remember that this SiteInstance has been used to load a URL, even if the
148 // URL is invalid.
149 has_site_ = true;
[email protected]4c3a23582012-08-18 08:54:34150 BrowserContext* browser_context = browsing_instance_->browser_context();
[email protected]41fb79a52012-06-29 16:34:33151 site_ = GetSiteForURL(browser_context, url);
initial.commit09911bf2008-07-26 23:55:29152
153 // Now that we have a site, register it with the BrowsingInstance. This
154 // ensures that we won't create another SiteInstance for this site within
155 // the same BrowsingInstance, because all same-site pages within a
156 // BrowsingInstance can script each other.
157 browsing_instance_->RegisterSiteInstance(this);
[email protected]313b80bd2011-11-23 03:49:10158
clamy63960c32017-05-10 22:57:07159 // Update the process reuse policy based on the site.
160 bool should_use_process_per_site =
161 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_);
162 if (should_use_process_per_site) {
163 process_reuse_policy_ = ProcessReusePolicy::PROCESS_PER_SITE;
164 }
165
[email protected]41fb79a52012-06-29 16:34:33166 if (process_) {
alexmos13fe1962017-06-28 04:25:12167 LockToOriginIfNeeded();
[email protected]41fb79a52012-06-29 16:34:33168
169 // Ensure the process is registered for this site if necessary.
clamy63960c32017-05-10 22:57:07170 if (should_use_process_per_site) {
[email protected]41fb79a52012-06-29 16:34:33171 RenderProcessHostImpl::RegisterProcessHostForSite(
172 browser_context, process_, site_);
173 }
174 }
initial.commit09911bf2008-07-26 23:55:29175}
176
[email protected]77ab17312012-09-28 15:34:59177const GURL& SiteInstanceImpl::GetSiteURL() const {
[email protected]b6583592012-01-25 19:52:33178 return site_;
179}
180
181bool SiteInstanceImpl::HasSite() const {
182 return has_site_;
183}
184
185bool SiteInstanceImpl::HasRelatedSiteInstance(const GURL& url) {
initial.commit09911bf2008-07-26 23:55:29186 return browsing_instance_->HasSiteInstance(url);
187}
188
dchengbccd6b82016-03-30 16:24:19189scoped_refptr<SiteInstance> SiteInstanceImpl::GetRelatedSiteInstance(
190 const GURL& url) {
initial.commit09911bf2008-07-26 23:55:29191 return browsing_instance_->GetSiteInstanceForURL(url);
192}
193
[email protected]14392a52012-05-02 20:28:44194bool SiteInstanceImpl::IsRelatedSiteInstance(const SiteInstance* instance) {
[email protected]fc72bb12013-06-02 21:13:46195 return browsing_instance_.get() == static_cast<const SiteInstanceImpl*>(
196 instance)->browsing_instance_.get();
[email protected]14392a52012-05-02 20:28:44197}
198
[email protected]d5072a82014-05-15 05:50:18199size_t SiteInstanceImpl::GetRelatedActiveContentsCount() {
200 return browsing_instance_->active_contents_count();
201}
202
[email protected]f88628d02012-11-11 17:58:59203bool SiteInstanceImpl::HasWrongProcessForURL(const GURL& url) {
[email protected]d292d8a2011-05-25 03:47:11204 // Having no process isn't a problem, since we'll assign it correctly.
[email protected]f88628d02012-11-11 17:58:59205 // Note that HasProcess() may return true if process_ is null, in
206 // process-per-site cases where there's an existing process available.
207 // We want to use such a process in the IsSuitableHost check, so we
208 // may end up assigning process_ in the GetProcess() call below.
209 if (!HasProcess())
[email protected]d292d8a2011-05-25 03:47:11210 return false;
211
[email protected]144a8102012-01-14 01:05:31212 // If the URL to navigate to can be associated with any site instance,
213 // we want to keep it in the same process.
[email protected]c02f1ba2014-02-03 06:53:53214 if (IsRendererDebugURL(url))
[email protected]144a8102012-01-14 01:05:31215 return false;
216
alexmos13fe1962017-06-28 04:25:12217 // Any process can host an about:blank URL. This check avoids a process
218 // transfer for browser-initiated navigations to about:blank in a dedicated
219 // process; without it, IsSuitableHost would consider this process unsuitable
220 // for about:blank when it compares origin locks. Renderer-initiated
221 // navigations will handle about:blank navigations elsewhere and leave them
222 // in the source SiteInstance, along with about:srcdoc and data:.
223 if (url == url::kAboutBlankURL)
224 return false;
225
[email protected]88aae972011-12-16 01:14:18226 // If the site URL is an extension (e.g., for hosted apps or WebUI) but the
[email protected]d292d8a2011-05-25 03:47:11227 // process is not (or vice versa), make sure we notice and fix it.
[email protected]2a5221b2011-09-27 23:07:31228 GURL site_url = GetSiteForURL(browsing_instance_->browser_context(), url);
[email protected]88aae972011-12-16 01:14:18229 return !RenderProcessHostImpl::IsSuitableHost(
[email protected]f88628d02012-11-11 17:58:59230 GetProcess(), browsing_instance_->browser_context(), site_url);
[email protected]d292d8a2011-05-25 03:47:11231}
232
nickd5bbd0b2016-03-31 19:52:44233scoped_refptr<SiteInstanceImpl>
234SiteInstanceImpl::GetDefaultSubframeSiteInstance() {
235 return browsing_instance_->GetDefaultSubframeSiteInstance();
236}
237
nickd30fd962015-07-27 21:51:08238bool SiteInstanceImpl::RequiresDedicatedProcess() {
239 if (!has_site_)
240 return false;
nickd5bbd0b2016-03-31 19:52:44241
242 return DoesSiteRequireDedicatedProcess(GetBrowserContext(), site_);
nickd30fd962015-07-27 21:51:08243}
244
avi85cacb72016-10-26 22:39:33245bool SiteInstanceImpl::IsDefaultSubframeSiteInstance() const {
clamy63960c32017-05-10 22:57:07246 return process_reuse_policy_ ==
247 ProcessReusePolicy::USE_DEFAULT_SUBFRAME_PROCESS;
avi85cacb72016-10-26 22:39:33248}
249
nick9f34e2892016-01-12 21:01:07250void SiteInstanceImpl::IncrementActiveFrameCount() {
251 active_frame_count_++;
252}
253
254void SiteInstanceImpl::DecrementActiveFrameCount() {
ericwilligers254597b2016-10-17 10:32:31255 if (--active_frame_count_ == 0) {
256 for (auto& observer : observers_)
257 observer.ActiveFrameCountIsZero(this);
258 }
nick9f34e2892016-01-12 21:01:07259}
260
[email protected]d5072a82014-05-15 05:50:18261void SiteInstanceImpl::IncrementRelatedActiveContentsCount() {
262 browsing_instance_->increment_active_contents_count();
263}
264
265void SiteInstanceImpl::DecrementRelatedActiveContentsCount() {
266 browsing_instance_->decrement_active_contents_count();
267}
268
nick9f34e2892016-01-12 21:01:07269void SiteInstanceImpl::AddObserver(Observer* observer) {
270 observers_.AddObserver(observer);
271}
272
273void SiteInstanceImpl::RemoveObserver(Observer* observer) {
274 observers_.RemoveObserver(observer);
275}
276
[email protected]4c3a23582012-08-18 08:54:34277BrowserContext* SiteInstanceImpl::GetBrowserContext() const {
[email protected]72daaa92012-01-18 13:39:02278 return browsing_instance_->browser_context();
279}
280
nickcc0d9142015-10-14 16:27:10281// static
dchengbccd6b82016-03-30 16:24:19282scoped_refptr<SiteInstance> SiteInstance::Create(
283 BrowserContext* browser_context) {
284 return SiteInstanceImpl::Create(browser_context);
initial.commit09911bf2008-07-26 23:55:29285}
286
nickcc0d9142015-10-14 16:27:10287// static
dchengbccd6b82016-03-30 16:24:19288scoped_refptr<SiteInstance> SiteInstance::CreateForURL(
289 BrowserContext* browser_context,
290 const GURL& url) {
291 return SiteInstanceImpl::CreateForURL(browser_context, url);
[email protected]d8a96622009-05-07 19:21:16292}
293
nickcc0d9142015-10-14 16:27:10294// static
[email protected]399583b2012-12-11 09:33:42295bool SiteInstance::IsSameWebSite(BrowserContext* browser_context,
[email protected]855d7d572014-08-02 11:18:17296 const GURL& real_src_url,
297 const GURL& real_dest_url) {
298 GURL src_url = SiteInstanceImpl::GetEffectiveURL(browser_context,
299 real_src_url);
300 GURL dest_url = SiteInstanceImpl::GetEffectiveURL(browser_context,
301 real_dest_url);
[email protected]399583b2012-12-11 09:33:42302
303 // We infer web site boundaries based on the registered domain name of the
304 // top-level page and the scheme. We do not pay attention to the port if
305 // one is present, because pages served from different ports can still
306 // access each other if they change their document.domain variable.
307
308 // Some special URLs will match the site instance of any other URL. This is
309 // done before checking both of them for validity, since we want these URLs
310 // to have the same site instance as even an invalid one.
[email protected]855d7d572014-08-02 11:18:17311 if (IsRendererDebugURL(src_url) || IsRendererDebugURL(dest_url))
[email protected]399583b2012-12-11 09:33:42312 return true;
313
314 // If either URL is invalid, they aren't part of the same site.
[email protected]855d7d572014-08-02 11:18:17315 if (!src_url.is_valid() || !dest_url.is_valid())
[email protected]399583b2012-12-11 09:33:42316 return false;
317
[email protected]855d7d572014-08-02 11:18:17318 // If the destination url is just a blank page, we treat them as part of the
319 // same site.
320 GURL blank_page(url::kAboutBlankURL);
321 if (dest_url == blank_page)
322 return true;
323
alexmos3b9ad102017-05-26 23:41:08324 url::Origin src_origin(src_url);
325 url::Origin dest_origin(dest_url);
alexmos3b9ad102017-05-26 23:41:08326
[email protected]399583b2012-12-11 09:33:42327 // If the schemes differ, they aren't part of the same site.
alexmoscbf995782017-06-01 03:13:13328 if (src_origin.scheme() != dest_origin.scheme())
[email protected]399583b2012-12-11 09:33:42329 return false;
330
alexmos4bc26322017-07-01 00:57:14331 if (!net::registry_controlled_domains::SameDomainOrHost(
332 src_origin, dest_origin,
333 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) {
334 return false;
335 }
336
337 // If the sites are the same, check isolated origins. If either URL matches
338 // an isolated origin, compare origins rather than sites.
339 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
340 url::Origin src_isolated_origin;
341 url::Origin dest_isolated_origin;
342 bool src_origin_is_isolated =
343 policy->GetMatchingIsolatedOrigin(src_origin, &src_isolated_origin);
344 bool dest_origin_is_isolated =
345 policy->GetMatchingIsolatedOrigin(dest_origin, &dest_isolated_origin);
346 if (src_origin_is_isolated || dest_origin_is_isolated) {
347 // Compare most specific matching origins to ensure that a subdomain of an
348 // isolated origin (e.g., https://subdomain.isolated.foo.com) also matches
349 // the isolated origin's site URL (e.g., https://isolated.foo.com).
350 return src_isolated_origin == dest_isolated_origin;
351 }
352
353 return true;
[email protected]399583b2012-12-11 09:33:42354}
355
nickcc0d9142015-10-14 16:27:10356// static
[email protected]399583b2012-12-11 09:33:42357GURL SiteInstance::GetSiteForURL(BrowserContext* browser_context,
358 const GURL& real_url) {
[email protected]25a9e8e532012-06-22 06:16:28359 // TODO(fsamuel, creis): For some reason appID is not recognized as a host.
[email protected]6eb1a11e2013-10-09 00:54:37360 if (real_url.SchemeIs(kGuestScheme))
[email protected]25a9e8e532012-06-22 06:16:28361 return real_url;
362
[email protected]b6583592012-01-25 19:52:33363 GURL url = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url);
nick1dd47922016-04-29 16:44:48364 url::Origin origin(url);
[email protected]3a8eecb2010-04-22 23:56:30365
alexmos4bc26322017-07-01 00:57:14366 // Isolated origins should use the full origin as their site URL. A subdomain
367 // of an isolated origin should also use that isolated origin's site URL.
alexmos3b9ad102017-05-26 23:41:08368 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
alexmos4bc26322017-07-01 00:57:14369 url::Origin isolated_origin;
370 if (policy->GetMatchingIsolatedOrigin(url::Origin(real_url),
371 &isolated_origin)) {
372 return isolated_origin.GetURL();
373 }
alexmos3b9ad102017-05-26 23:41:08374
initial.commit09911bf2008-07-26 23:55:29375 // If the url has a host, then determine the site.
nick1dd47922016-04-29 16:44:48376 if (!origin.host().empty()) {
377 // Only keep the scheme and registered domain of |origin|.
378 std::string domain = net::registry_controlled_domains::GetDomainAndRegistry(
379 origin.host(),
380 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
381 std::string site = origin.scheme();
382 site += url::kStandardSchemeSeparator;
383 site += domain.empty() ? origin.host() : domain;
384 return GURL(site);
initial.commit09911bf2008-07-26 23:55:29385 }
creisf60c2cd2014-12-18 00:41:02386
387 // If there is no host but there is a scheme, return the scheme.
388 // This is useful for cases like file URLs.
389 if (url.has_scheme())
390 return GURL(url.scheme() + ":");
391
392 // Otherwise the URL should be invalid; return an empty site.
393 DCHECK(!url.is_valid());
394 return GURL();
initial.commit09911bf2008-07-26 23:55:29395}
396
nickcc0d9142015-10-14 16:27:10397// static
[email protected]4c3a23582012-08-18 08:54:34398GURL SiteInstanceImpl::GetEffectiveURL(BrowserContext* browser_context,
399 const GURL& url) {
alexmos3b9ad102017-05-26 23:41:08400 // Don't resolve URLs corresponding to isolated origins, as isolated origins
401 // take precedence over hosted apps.
402 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
403 if (policy->IsIsolatedOrigin(url::Origin(url)))
404 return url;
405
[email protected]46488322012-10-30 03:22:20406 return GetContentClient()->browser()->
[email protected]3d7474ff2011-07-27 17:47:37407 GetEffectiveURL(browser_context, url);
[email protected]3a8eecb2010-04-22 23:56:30408}
409
nickcc0d9142015-10-14 16:27:10410// static
411bool SiteInstanceImpl::DoesSiteRequireDedicatedProcess(
412 BrowserContext* browser_context,
nickdb193a12016-09-09 23:09:23413 const GURL& url) {
nickcc0d9142015-10-14 16:27:10414 // If --site-per-process is enabled, site isolation is enabled everywhere.
415 if (SiteIsolationPolicy::UseDedicatedProcessesForAllSites())
416 return true;
417
alexmos3b9ad102017-05-26 23:41:08418 // Always require a dedicated process for isolated origins.
419 GURL site_url = GetSiteForURL(browser_context, url);
420 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
421 if (policy->IsIsolatedOrigin(url::Origin(site_url)))
422 return true;
423
nickdb193a12016-09-09 23:09:23424 // Let the content embedder enable site isolation for specific URLs. Use the
425 // canonical site url for this check, so that schemes with nested origins
426 // (blob and filesystem) work properly.
nickcc0d9142015-10-14 16:27:10427 if (GetContentClient()->IsSupplementarySiteIsolationModeEnabled() &&
428 GetContentClient()->browser()->DoesSiteRequireDedicatedProcess(
nickdb193a12016-09-09 23:09:23429 browser_context, site_url)) {
nickcc0d9142015-10-14 16:27:10430 return true;
431 }
432
433 return false;
434}
435
alexmos13fe1962017-06-28 04:25:12436// static
437bool SiteInstanceImpl::ShouldLockToOrigin(BrowserContext* browser_context,
Alex Moshchuk9cf5a45d2017-08-14 18:50:57438 RenderProcessHost* host,
alexmos13fe1962017-06-28 04:25:12439 GURL site_url) {
creis0b49fa12017-07-10 16:31:11440 // Don't lock to origin in --single-process mode, since this mode puts
441 // cross-site pages into the same process.
Alex Moshchuk9cf5a45d2017-08-14 18:50:57442 if (host->run_renderer_in_process())
creis0b49fa12017-07-10 16:31:11443 return false;
444
alexmos13fe1962017-06-28 04:25:12445 if (!DoesSiteRequireDedicatedProcess(browser_context, site_url))
446 return false;
447
448 // Guest processes cannot be locked to their site because guests always have
449 // a fixed SiteInstance. The site of GURLs a guest loads doesn't match that
450 // SiteInstance. So we skip locking the guest process to the site.
451 // TODO(ncarter): Remove this exclusion once we can make origin lock per
452 // RenderFrame routing id.
453 if (site_url.SchemeIs(content::kGuestScheme))
454 return false;
455
456 // TODO(creis, nick) https://crbug.com/510588 Chrome UI pages use the same
457 // site (chrome://chrome), so they can't be locked because the site being
458 // loaded doesn't match the SiteInstance.
459 if (site_url.SchemeIs(content::kChromeUIScheme))
460 return false;
461
462 // TODO(creis, nick): Until we can handle sites with effective URLs at the
463 // call sites of ChildProcessSecurityPolicy::CanAccessDataForOrigin, we
464 // must give the embedder a chance to exempt some sites to avoid process
465 // kills.
466 if (!GetContentClient()->browser()->ShouldLockToOrigin(browser_context,
467 site_url)) {
468 return false;
469 }
470
471 return true;
472}
473
Alex Moshchuk54bf13042017-10-13 04:25:29474// static
475bool SiteInstanceImpl::ShouldAssignSiteForURL(const GURL& url) {
476 // about:blank should not "use up" a new SiteInstance. The SiteInstance can
477 // still be used for a normal web site.
478 if (url == url::kAboutBlankURL)
479 return false;
480
481 // The embedder will then have the opportunity to determine if the URL
482 // should "use up" the SiteInstance.
483 return GetContentClient()->browser()->ShouldAssignSiteForURL(url);
484}
485
[email protected]40bfaf32013-11-19 01:35:43486void SiteInstanceImpl::RenderProcessHostDestroyed(RenderProcessHost* host) {
487 DCHECK_EQ(process_, host);
488 process_->RemoveObserver(this);
nickd5bbd0b2016-03-31 19:52:44489 process_ = nullptr;
[email protected]4566f132009-03-12 01:55:13490}
[email protected]313b80bd2011-11-23 03:49:10491
nick9f34e2892016-01-12 21:01:07492void SiteInstanceImpl::RenderProcessWillExit(RenderProcessHost* host) {
493 // TODO(nick): http://crbug.com/575400 - RenderProcessWillExit might not serve
494 // any purpose here.
ericwilligers254597b2016-10-17 10:32:31495 for (auto& observer : observers_)
496 observer.RenderProcessGone(this);
nick9f34e2892016-01-12 21:01:07497}
498
499void SiteInstanceImpl::RenderProcessExited(RenderProcessHost* host,
500 base::TerminationStatus status,
501 int exit_code) {
ericwilligers254597b2016-10-17 10:32:31502 for (auto& observer : observers_)
503 observer.RenderProcessGone(this);
nick9f34e2892016-01-12 21:01:07504}
505
alexmos13fe1962017-06-28 04:25:12506void SiteInstanceImpl::LockToOriginIfNeeded() {
507 DCHECK(HasSite());
508
509 // From now on, this process should be considered "tainted" for future
510 // process reuse decisions:
511 // (1) If |site_| required a dedicated process, this SiteInstance's process
512 // can only host URLs for the same site.
513 // (2) Even if |site_| does not require a dedicated process, this
514 // SiteInstance's process still cannot be reused to host other sites
515 // requiring dedicated sites in the future.
516 // We can get here either when we commit a URL into a SiteInstance that does
517 // not yet have a site, or when we create a process for a SiteInstance with a
518 // preassigned site.
alexmos13fe1962017-06-28 04:25:12519 process_->SetIsUsed();
520
Alex Moshchuk47dd00f2017-10-10 17:15:23521 ChildProcessSecurityPolicyImpl* policy =
522 ChildProcessSecurityPolicyImpl::GetInstance();
523 auto lock_state = policy->CheckOriginLock(process_->GetID(), site_);
Alex Moshchuk9cf5a45d2017-08-14 18:50:57524 if (ShouldLockToOrigin(GetBrowserContext(), process_, site_)) {
Alex Moshchuk9cf5a45d2017-08-14 18:50:57525 // Sanity check that this won't try to assign an origin lock to a <webview>
526 // process, which can't be locked.
527 CHECK(!process_->IsForGuestsOnly());
528
Alex Moshchuk9cf5a45d2017-08-14 18:50:57529 switch (lock_state) {
530 case ChildProcessSecurityPolicyImpl::CheckOriginLockResult::NO_LOCK: {
Alex Moshchuk47dd00f2017-10-10 17:15:23531 // TODO(nick): When all sites are isolated, this operation provides
532 // strong protection. If only some sites are isolated, we need
533 // additional logic to prevent the non-isolated sites from requesting
534 // resources for isolated sites. https://crbug.com/509125
Alex Moshchuk9cf5a45d2017-08-14 18:50:57535 policy->LockToOrigin(process_->GetID(), site_);
536 break;
537 }
538 case ChildProcessSecurityPolicyImpl::CheckOriginLockResult::
539 HAS_WRONG_LOCK:
540 // We should never attempt to reassign a different origin lock to a
541 // process.
Alex Moshchuk54bf13042017-10-13 04:25:29542 CHECK(false) << "Trying to lock a process to " << site_
543 << " but the process is already locked to "
544 << policy->GetOriginLock(process_->GetID());
Alex Moshchuk9cf5a45d2017-08-14 18:50:57545 break;
546 case ChildProcessSecurityPolicyImpl::CheckOriginLockResult::
547 HAS_EQUAL_LOCK:
548 // Process already has the right origin lock assigned. This case will
549 // happen for commits to |site_| after the first one.
550 break;
551 default:
552 NOTREACHED();
553 }
Alex Moshchuk47dd00f2017-10-10 17:15:23554 } else {
555 // If the site that we've just committed doesn't require a dedicated
556 // process, make sure we aren't putting it in a process for a site that
557 // does.
558 CHECK_EQ(lock_state,
Alex Moshchuk54bf13042017-10-13 04:25:29559 ChildProcessSecurityPolicyImpl::CheckOriginLockResult::NO_LOCK)
560 << "Trying to commit non-isolated site " << site_
561 << " in process locked to " << policy->GetOriginLock(process_->GetID());
[email protected]313b80bd2011-11-23 03:49:10562 }
563}
[email protected]46488322012-10-30 03:22:20564
565} // namespace content