blob: 8afc081c5fe98acda759c0b9a82444a08eacfa32 [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
alexmos3b9ad102017-05-26 23:41:087#include "base/macros.h"
avicb129c02017-05-03 06:49:298#include "base/memory/ptr_util.h"
[email protected]39365212011-02-24 01:01:009#include "content/browser/browsing_instance.h"
[email protected]b9535422012-02-09 01:47:5910#include "content/browser/child_process_security_policy_impl.h"
[email protected]c02f1ba2014-02-03 06:53:5311#include "content/browser/frame_host/debug_urls.h"
nick9f34e2892016-01-12 21:01:0712#include "content/browser/frame_host/frame_tree_node.h"
[email protected]f3b1a082011-11-18 00:34:3013#include "content/browser/renderer_host/render_process_host_impl.h"
[email protected]4c3a23582012-08-18 08:54:3414#include "content/browser/storage_partition_impl.h"
nickd30fd962015-07-27 21:51:0815#include "content/common/site_isolation_policy.h"
[email protected]87f3c082011-10-19 18:07:4416#include "content/public/browser/content_browser_client.h"
[email protected]f3b1a082011-11-18 00:34:3017#include "content/public/browser/render_process_host_factory.h"
[email protected]41fb79a52012-06-29 16:34:3318#include "content/public/browser/web_ui_controller_factory.h"
[email protected]a1d29162011-10-14 17:14:0319#include "content/public/common/url_constants.h"
[email protected]be28b5f42012-07-20 11:31:2520#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
initial.commit09911bf2008-07-26 23:55:2921
[email protected]46488322012-10-30 03:22:2022namespace content {
[email protected]b6583592012-01-25 19:52:3323
avib7348942015-12-25 20:57:1024int32_t SiteInstanceImpl::next_site_instance_id_ = 1;
[email protected]992db4c2011-05-12 15:37:1525
[email protected]b6583592012-01-25 19:52:3326SiteInstanceImpl::SiteInstanceImpl(BrowsingInstance* browsing_instance)
[email protected]992db4c2011-05-12 15:37:1527 : id_(next_site_instance_id_++),
creiscce56cd2014-09-29 22:45:2228 active_frame_count_(0),
[email protected]992db4c2011-05-12 15:37:1529 browsing_instance_(browsing_instance),
nickd5bbd0b2016-03-31 19:52:4430 process_(nullptr),
31 has_site_(false),
Tsuyoshi Horoeb576e62017-06-28 06:00:4132 process_reuse_policy_(ProcessReusePolicy::DEFAULT),
33 is_for_service_worker_(false) {
[email protected]4566f132009-03-12 01:55:1334 DCHECK(browsing_instance);
[email protected]4566f132009-03-12 01:55:1335}
36
[email protected]b6583592012-01-25 19:52:3337SiteInstanceImpl::~SiteInstanceImpl() {
[email protected]46488322012-10-30 03:22:2038 GetContentClient()->browser()->SiteInstanceDeleting(this);
[email protected]056ad2a2011-07-12 02:13:5539
[email protected]40bfaf32013-11-19 01:35:4340 if (process_)
41 process_->RemoveObserver(this);
42
initial.commit09911bf2008-07-26 23:55:2943 // Now that no one is referencing us, we can safely remove ourselves from
44 // the BrowsingInstance. Any future visits to a page from this site
45 // (within the same BrowsingInstance) can safely create a new SiteInstance.
46 if (has_site_)
dchengbccd6b82016-03-30 16:24:1947 browsing_instance_->UnregisterSiteInstance(this);
48}
49
50scoped_refptr<SiteInstanceImpl> SiteInstanceImpl::Create(
51 BrowserContext* browser_context) {
52 return make_scoped_refptr(
53 new SiteInstanceImpl(new BrowsingInstance(browser_context)));
54}
55
56scoped_refptr<SiteInstanceImpl> SiteInstanceImpl::CreateForURL(
57 BrowserContext* browser_context,
58 const GURL& url) {
59 // This will create a new SiteInstance and BrowsingInstance.
60 scoped_refptr<BrowsingInstance> instance(
61 new BrowsingInstance(browser_context));
62 return instance->GetSiteInstanceForURL(url);
initial.commit09911bf2008-07-26 23:55:2963}
64
avib7348942015-12-25 20:57:1065int32_t SiteInstanceImpl::GetId() {
[email protected]b6583592012-01-25 19:52:3366 return id_;
67}
68
69bool SiteInstanceImpl::HasProcess() const {
[email protected]41fb79a52012-06-29 16:34:3370 if (process_ != NULL)
71 return true;
72
73 // If we would use process-per-site for this site, also check if there is an
74 // existing process that we would use if GetProcess() were called.
[email protected]46488322012-10-30 03:22:2075 BrowserContext* browser_context =
[email protected]41fb79a52012-06-29 16:34:3376 browsing_instance_->browser_context();
77 if (has_site_ &&
[email protected]1ae93fb12013-06-14 03:38:5678 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_) &&
[email protected]41fb79a52012-06-29 16:34:3379 RenderProcessHostImpl::GetProcessHostForSite(browser_context, site_)) {
80 return true;
81 }
82
83 return false;
[email protected]5ab79b02010-04-26 16:47:1184}
85
[email protected]41fb79a52012-06-29 16:34:3386RenderProcessHost* SiteInstanceImpl::GetProcess() {
[email protected]08c8ec7e2010-07-11 17:14:4887 // TODO(erikkay) It would be nice to ensure that the renderer type had been
88 // properly set before we get here. The default tab creation case winds up
89 // with no site set at this point, so it will default to TYPE_NORMAL. This
90 // may not be correct, so we'll wind up potentially creating a process that
91 // we then throw away, or worse sharing a process with the wrong process type.
92 // See crbug.com/43448.
93
initial.commit09911bf2008-07-26 23:55:2994 // Create a new process if ours went away or was reused.
[email protected]4566f132009-03-12 01:55:1395 if (!process_) {
[email protected]4c3a23582012-08-18 08:54:3496 BrowserContext* browser_context = browsing_instance_->browser_context();
[email protected]41fb79a52012-06-29 16:34:3397
clamy63960c32017-05-10 22:57:0798 // Check if the ProcessReusePolicy should be updated.
99 bool should_use_process_per_site =
creisa9f04ba2017-05-19 22:27:42100 has_site_ &&
101 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_);
clamy63960c32017-05-10 22:57:07102 if (should_use_process_per_site) {
103 process_reuse_policy_ = ProcessReusePolicy::PROCESS_PER_SITE;
104 } else if (process_reuse_policy_ == ProcessReusePolicy::PROCESS_PER_SITE) {
105 process_reuse_policy_ = ProcessReusePolicy::DEFAULT;
[email protected]41fb79a52012-06-29 16:34:33106 }
107
clamy63960c32017-05-10 22:57:07108 process_ = RenderProcessHostImpl::GetProcessHostForSiteInstance(
109 browser_context, this);
avi6d686db2017-01-06 02:28:57110
[email protected]41fb79a52012-06-29 16:34:33111 CHECK(process_);
[email protected]40bfaf32013-11-19 01:35:43112 process_->AddObserver(this);
[email protected]41fb79a52012-06-29 16:34:33113
114 // If we are using process-per-site, we need to register this process
115 // for the current site so that we can find it again. (If no site is set
116 // at this time, we will register it in SetSite().)
clamy63960c32017-05-10 22:57:07117 if (process_reuse_policy_ == ProcessReusePolicy::PROCESS_PER_SITE &&
118 has_site_) {
[email protected]41fb79a52012-06-29 16:34:33119 RenderProcessHostImpl::RegisterProcessHostForSite(browser_context,
120 process_, site_);
121 }
initial.commit09911bf2008-07-26 23:55:29122
naskob8744d22014-08-28 17:07:43123 TRACE_EVENT2("navigation", "SiteInstanceImpl::GetProcess",
124 "site id", id_, "process id", process_->GetID());
[email protected]46488322012-10-30 03:22:20125 GetContentClient()->browser()->SiteInstanceGotProcess(this);
[email protected]6f371442011-11-09 06:45:46126
[email protected]313b80bd2011-11-23 03:49:10127 if (has_site_)
alexmos13fe1962017-06-28 04:25:12128 LockToOriginIfNeeded();
initial.commit09911bf2008-07-26 23:55:29129 }
[email protected]4566f132009-03-12 01:55:13130 DCHECK(process_);
initial.commit09911bf2008-07-26 23:55:29131
[email protected]4566f132009-03-12 01:55:13132 return process_;
initial.commit09911bf2008-07-26 23:55:29133}
134
[email protected]b6583592012-01-25 19:52:33135void SiteInstanceImpl::SetSite(const GURL& url) {
naskob8744d22014-08-28 17:07:43136 TRACE_EVENT2("navigation", "SiteInstanceImpl::SetSite",
137 "site id", id_, "url", url.possibly_invalid_spec());
initial.commit09911bf2008-07-26 23:55:29138 // A SiteInstance's site should not change.
139 // TODO(creis): When following links or script navigations, we can currently
140 // render pages from other sites in this SiteInstance. This will eventually
141 // be fixed, but until then, we should still not set the site of a
142 // SiteInstance more than once.
143 DCHECK(!has_site_);
144
145 // Remember that this SiteInstance has been used to load a URL, even if the
146 // URL is invalid.
147 has_site_ = true;
[email protected]4c3a23582012-08-18 08:54:34148 BrowserContext* browser_context = browsing_instance_->browser_context();
[email protected]41fb79a52012-06-29 16:34:33149 site_ = GetSiteForURL(browser_context, url);
initial.commit09911bf2008-07-26 23:55:29150
151 // Now that we have a site, register it with the BrowsingInstance. This
152 // ensures that we won't create another SiteInstance for this site within
153 // the same BrowsingInstance, because all same-site pages within a
154 // BrowsingInstance can script each other.
155 browsing_instance_->RegisterSiteInstance(this);
[email protected]313b80bd2011-11-23 03:49:10156
clamy63960c32017-05-10 22:57:07157 // Update the process reuse policy based on the site.
158 bool should_use_process_per_site =
159 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_);
160 if (should_use_process_per_site) {
161 process_reuse_policy_ = ProcessReusePolicy::PROCESS_PER_SITE;
162 }
163
[email protected]41fb79a52012-06-29 16:34:33164 if (process_) {
alexmos13fe1962017-06-28 04:25:12165 LockToOriginIfNeeded();
[email protected]41fb79a52012-06-29 16:34:33166
167 // Ensure the process is registered for this site if necessary.
clamy63960c32017-05-10 22:57:07168 if (should_use_process_per_site) {
[email protected]41fb79a52012-06-29 16:34:33169 RenderProcessHostImpl::RegisterProcessHostForSite(
170 browser_context, process_, site_);
171 }
172 }
initial.commit09911bf2008-07-26 23:55:29173}
174
[email protected]77ab17312012-09-28 15:34:59175const GURL& SiteInstanceImpl::GetSiteURL() const {
[email protected]b6583592012-01-25 19:52:33176 return site_;
177}
178
179bool SiteInstanceImpl::HasSite() const {
180 return has_site_;
181}
182
183bool SiteInstanceImpl::HasRelatedSiteInstance(const GURL& url) {
initial.commit09911bf2008-07-26 23:55:29184 return browsing_instance_->HasSiteInstance(url);
185}
186
dchengbccd6b82016-03-30 16:24:19187scoped_refptr<SiteInstance> SiteInstanceImpl::GetRelatedSiteInstance(
188 const GURL& url) {
initial.commit09911bf2008-07-26 23:55:29189 return browsing_instance_->GetSiteInstanceForURL(url);
190}
191
[email protected]14392a52012-05-02 20:28:44192bool SiteInstanceImpl::IsRelatedSiteInstance(const SiteInstance* instance) {
[email protected]fc72bb12013-06-02 21:13:46193 return browsing_instance_.get() == static_cast<const SiteInstanceImpl*>(
194 instance)->browsing_instance_.get();
[email protected]14392a52012-05-02 20:28:44195}
196
[email protected]d5072a82014-05-15 05:50:18197size_t SiteInstanceImpl::GetRelatedActiveContentsCount() {
198 return browsing_instance_->active_contents_count();
199}
200
[email protected]f88628d02012-11-11 17:58:59201bool SiteInstanceImpl::HasWrongProcessForURL(const GURL& url) {
[email protected]d292d8a2011-05-25 03:47:11202 // Having no process isn't a problem, since we'll assign it correctly.
[email protected]f88628d02012-11-11 17:58:59203 // Note that HasProcess() may return true if process_ is null, in
204 // process-per-site cases where there's an existing process available.
205 // We want to use such a process in the IsSuitableHost check, so we
206 // may end up assigning process_ in the GetProcess() call below.
207 if (!HasProcess())
[email protected]d292d8a2011-05-25 03:47:11208 return false;
209
[email protected]144a8102012-01-14 01:05:31210 // If the URL to navigate to can be associated with any site instance,
211 // we want to keep it in the same process.
[email protected]c02f1ba2014-02-03 06:53:53212 if (IsRendererDebugURL(url))
[email protected]144a8102012-01-14 01:05:31213 return false;
214
alexmos13fe1962017-06-28 04:25:12215 // Any process can host an about:blank URL. This check avoids a process
216 // transfer for browser-initiated navigations to about:blank in a dedicated
217 // process; without it, IsSuitableHost would consider this process unsuitable
218 // for about:blank when it compares origin locks. Renderer-initiated
219 // navigations will handle about:blank navigations elsewhere and leave them
220 // in the source SiteInstance, along with about:srcdoc and data:.
221 if (url == url::kAboutBlankURL)
222 return false;
223
[email protected]88aae972011-12-16 01:14:18224 // If the site URL is an extension (e.g., for hosted apps or WebUI) but the
[email protected]d292d8a2011-05-25 03:47:11225 // process is not (or vice versa), make sure we notice and fix it.
[email protected]2a5221b2011-09-27 23:07:31226 GURL site_url = GetSiteForURL(browsing_instance_->browser_context(), url);
[email protected]88aae972011-12-16 01:14:18227 return !RenderProcessHostImpl::IsSuitableHost(
[email protected]f88628d02012-11-11 17:58:59228 GetProcess(), browsing_instance_->browser_context(), site_url);
[email protected]d292d8a2011-05-25 03:47:11229}
230
nickd5bbd0b2016-03-31 19:52:44231scoped_refptr<SiteInstanceImpl>
232SiteInstanceImpl::GetDefaultSubframeSiteInstance() {
233 return browsing_instance_->GetDefaultSubframeSiteInstance();
234}
235
nickd30fd962015-07-27 21:51:08236bool SiteInstanceImpl::RequiresDedicatedProcess() {
237 if (!has_site_)
238 return false;
nickd5bbd0b2016-03-31 19:52:44239
240 return DoesSiteRequireDedicatedProcess(GetBrowserContext(), site_);
nickd30fd962015-07-27 21:51:08241}
242
avi85cacb72016-10-26 22:39:33243bool SiteInstanceImpl::IsDefaultSubframeSiteInstance() const {
clamy63960c32017-05-10 22:57:07244 return process_reuse_policy_ ==
245 ProcessReusePolicy::USE_DEFAULT_SUBFRAME_PROCESS;
avi85cacb72016-10-26 22:39:33246}
247
nick9f34e2892016-01-12 21:01:07248void SiteInstanceImpl::IncrementActiveFrameCount() {
249 active_frame_count_++;
250}
251
252void SiteInstanceImpl::DecrementActiveFrameCount() {
ericwilligers254597b2016-10-17 10:32:31253 if (--active_frame_count_ == 0) {
254 for (auto& observer : observers_)
255 observer.ActiveFrameCountIsZero(this);
256 }
nick9f34e2892016-01-12 21:01:07257}
258
[email protected]d5072a82014-05-15 05:50:18259void SiteInstanceImpl::IncrementRelatedActiveContentsCount() {
260 browsing_instance_->increment_active_contents_count();
261}
262
263void SiteInstanceImpl::DecrementRelatedActiveContentsCount() {
264 browsing_instance_->decrement_active_contents_count();
265}
266
nick9f34e2892016-01-12 21:01:07267void SiteInstanceImpl::AddObserver(Observer* observer) {
268 observers_.AddObserver(observer);
269}
270
271void SiteInstanceImpl::RemoveObserver(Observer* observer) {
272 observers_.RemoveObserver(observer);
273}
274
[email protected]4c3a23582012-08-18 08:54:34275BrowserContext* SiteInstanceImpl::GetBrowserContext() const {
[email protected]72daaa92012-01-18 13:39:02276 return browsing_instance_->browser_context();
277}
278
nickcc0d9142015-10-14 16:27:10279// static
dchengbccd6b82016-03-30 16:24:19280scoped_refptr<SiteInstance> SiteInstance::Create(
281 BrowserContext* browser_context) {
282 return SiteInstanceImpl::Create(browser_context);
initial.commit09911bf2008-07-26 23:55:29283}
284
nickcc0d9142015-10-14 16:27:10285// static
dchengbccd6b82016-03-30 16:24:19286scoped_refptr<SiteInstance> SiteInstance::CreateForURL(
287 BrowserContext* browser_context,
288 const GURL& url) {
289 return SiteInstanceImpl::CreateForURL(browser_context, url);
[email protected]d8a96622009-05-07 19:21:16290}
291
nickcc0d9142015-10-14 16:27:10292// static
[email protected]399583b2012-12-11 09:33:42293bool SiteInstance::IsSameWebSite(BrowserContext* browser_context,
[email protected]855d7d572014-08-02 11:18:17294 const GURL& real_src_url,
295 const GURL& real_dest_url) {
296 GURL src_url = SiteInstanceImpl::GetEffectiveURL(browser_context,
297 real_src_url);
298 GURL dest_url = SiteInstanceImpl::GetEffectiveURL(browser_context,
299 real_dest_url);
[email protected]399583b2012-12-11 09:33:42300
301 // We infer web site boundaries based on the registered domain name of the
302 // top-level page and the scheme. We do not pay attention to the port if
303 // one is present, because pages served from different ports can still
304 // access each other if they change their document.domain variable.
305
306 // Some special URLs will match the site instance of any other URL. This is
307 // done before checking both of them for validity, since we want these URLs
308 // to have the same site instance as even an invalid one.
[email protected]855d7d572014-08-02 11:18:17309 if (IsRendererDebugURL(src_url) || IsRendererDebugURL(dest_url))
[email protected]399583b2012-12-11 09:33:42310 return true;
311
312 // If either URL is invalid, they aren't part of the same site.
[email protected]855d7d572014-08-02 11:18:17313 if (!src_url.is_valid() || !dest_url.is_valid())
[email protected]399583b2012-12-11 09:33:42314 return false;
315
[email protected]855d7d572014-08-02 11:18:17316 // If the destination url is just a blank page, we treat them as part of the
317 // same site.
318 GURL blank_page(url::kAboutBlankURL);
319 if (dest_url == blank_page)
320 return true;
321
alexmos3b9ad102017-05-26 23:41:08322 // If either URL has an isolated origin, compare origins rather than sites.
323 url::Origin src_origin(src_url);
324 url::Origin dest_origin(dest_url);
325 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
326 if (policy->IsIsolatedOrigin(src_origin) ||
327 policy->IsIsolatedOrigin(dest_origin))
328 return src_origin == dest_origin;
329
[email protected]399583b2012-12-11 09:33:42330 // If the schemes differ, they aren't part of the same site.
alexmoscbf995782017-06-01 03:13:13331 if (src_origin.scheme() != dest_origin.scheme())
[email protected]399583b2012-12-11 09:33:42332 return false;
333
[email protected]ed32c212013-05-14 20:49:29334 return net::registry_controlled_domains::SameDomainOrHost(
alexmoscbf995782017-06-01 03:13:13335 src_origin, dest_origin,
[email protected]aabe1792014-01-30 21:37:46336 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
[email protected]399583b2012-12-11 09:33:42337}
338
nickcc0d9142015-10-14 16:27:10339// static
[email protected]399583b2012-12-11 09:33:42340GURL SiteInstance::GetSiteForURL(BrowserContext* browser_context,
341 const GURL& real_url) {
[email protected]25a9e8e532012-06-22 06:16:28342 // TODO(fsamuel, creis): For some reason appID is not recognized as a host.
[email protected]6eb1a11e2013-10-09 00:54:37343 if (real_url.SchemeIs(kGuestScheme))
[email protected]25a9e8e532012-06-22 06:16:28344 return real_url;
345
[email protected]b6583592012-01-25 19:52:33346 GURL url = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url);
nick1dd47922016-04-29 16:44:48347 url::Origin origin(url);
[email protected]3a8eecb2010-04-22 23:56:30348
alexmos3b9ad102017-05-26 23:41:08349 // Isolated origins should use the full origin as their site URL.
350 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
351 if (policy->IsIsolatedOrigin(origin))
352 return origin.GetURL();
353
initial.commit09911bf2008-07-26 23:55:29354 // If the url has a host, then determine the site.
nick1dd47922016-04-29 16:44:48355 if (!origin.host().empty()) {
356 // Only keep the scheme and registered domain of |origin|.
357 std::string domain = net::registry_controlled_domains::GetDomainAndRegistry(
358 origin.host(),
359 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
360 std::string site = origin.scheme();
361 site += url::kStandardSchemeSeparator;
362 site += domain.empty() ? origin.host() : domain;
363 return GURL(site);
initial.commit09911bf2008-07-26 23:55:29364 }
creisf60c2cd2014-12-18 00:41:02365
366 // If there is no host but there is a scheme, return the scheme.
367 // This is useful for cases like file URLs.
368 if (url.has_scheme())
369 return GURL(url.scheme() + ":");
370
371 // Otherwise the URL should be invalid; return an empty site.
372 DCHECK(!url.is_valid());
373 return GURL();
initial.commit09911bf2008-07-26 23:55:29374}
375
nickcc0d9142015-10-14 16:27:10376// static
[email protected]4c3a23582012-08-18 08:54:34377GURL SiteInstanceImpl::GetEffectiveURL(BrowserContext* browser_context,
378 const GURL& url) {
alexmos3b9ad102017-05-26 23:41:08379 // Don't resolve URLs corresponding to isolated origins, as isolated origins
380 // take precedence over hosted apps.
381 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
382 if (policy->IsIsolatedOrigin(url::Origin(url)))
383 return url;
384
[email protected]46488322012-10-30 03:22:20385 return GetContentClient()->browser()->
[email protected]3d7474ff2011-07-27 17:47:37386 GetEffectiveURL(browser_context, url);
[email protected]3a8eecb2010-04-22 23:56:30387}
388
nickcc0d9142015-10-14 16:27:10389// static
390bool SiteInstanceImpl::DoesSiteRequireDedicatedProcess(
391 BrowserContext* browser_context,
nickdb193a12016-09-09 23:09:23392 const GURL& url) {
nickcc0d9142015-10-14 16:27:10393 // If --site-per-process is enabled, site isolation is enabled everywhere.
394 if (SiteIsolationPolicy::UseDedicatedProcessesForAllSites())
395 return true;
396
alexmos3b9ad102017-05-26 23:41:08397 // Always require a dedicated process for isolated origins.
398 GURL site_url = GetSiteForURL(browser_context, url);
399 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
400 if (policy->IsIsolatedOrigin(url::Origin(site_url)))
401 return true;
402
nickdb193a12016-09-09 23:09:23403 // Let the content embedder enable site isolation for specific URLs. Use the
404 // canonical site url for this check, so that schemes with nested origins
405 // (blob and filesystem) work properly.
nickcc0d9142015-10-14 16:27:10406 if (GetContentClient()->IsSupplementarySiteIsolationModeEnabled() &&
407 GetContentClient()->browser()->DoesSiteRequireDedicatedProcess(
nickdb193a12016-09-09 23:09:23408 browser_context, site_url)) {
nickcc0d9142015-10-14 16:27:10409 return true;
410 }
411
412 return false;
413}
414
alexmos13fe1962017-06-28 04:25:12415// static
416bool SiteInstanceImpl::ShouldLockToOrigin(BrowserContext* browser_context,
417 GURL site_url) {
418 if (!DoesSiteRequireDedicatedProcess(browser_context, site_url))
419 return false;
420
421 // Guest processes cannot be locked to their site because guests always have
422 // a fixed SiteInstance. The site of GURLs a guest loads doesn't match that
423 // SiteInstance. So we skip locking the guest process to the site.
424 // TODO(ncarter): Remove this exclusion once we can make origin lock per
425 // RenderFrame routing id.
426 if (site_url.SchemeIs(content::kGuestScheme))
427 return false;
428
429 // TODO(creis, nick) https://crbug.com/510588 Chrome UI pages use the same
430 // site (chrome://chrome), so they can't be locked because the site being
431 // loaded doesn't match the SiteInstance.
432 if (site_url.SchemeIs(content::kChromeUIScheme))
433 return false;
434
435 // TODO(creis, nick): Until we can handle sites with effective URLs at the
436 // call sites of ChildProcessSecurityPolicy::CanAccessDataForOrigin, we
437 // must give the embedder a chance to exempt some sites to avoid process
438 // kills.
439 if (!GetContentClient()->browser()->ShouldLockToOrigin(browser_context,
440 site_url)) {
441 return false;
442 }
443
444 return true;
445}
446
[email protected]40bfaf32013-11-19 01:35:43447void SiteInstanceImpl::RenderProcessHostDestroyed(RenderProcessHost* host) {
448 DCHECK_EQ(process_, host);
449 process_->RemoveObserver(this);
nickd5bbd0b2016-03-31 19:52:44450 process_ = nullptr;
[email protected]4566f132009-03-12 01:55:13451}
[email protected]313b80bd2011-11-23 03:49:10452
nick9f34e2892016-01-12 21:01:07453void SiteInstanceImpl::RenderProcessWillExit(RenderProcessHost* host) {
454 // TODO(nick): http://crbug.com/575400 - RenderProcessWillExit might not serve
455 // any purpose here.
ericwilligers254597b2016-10-17 10:32:31456 for (auto& observer : observers_)
457 observer.RenderProcessGone(this);
nick9f34e2892016-01-12 21:01:07458}
459
460void SiteInstanceImpl::RenderProcessExited(RenderProcessHost* host,
461 base::TerminationStatus status,
462 int exit_code) {
ericwilligers254597b2016-10-17 10:32:31463 for (auto& observer : observers_)
464 observer.RenderProcessGone(this);
nick9f34e2892016-01-12 21:01:07465}
466
alexmos13fe1962017-06-28 04:25:12467void SiteInstanceImpl::LockToOriginIfNeeded() {
468 DCHECK(HasSite());
469
470 // From now on, this process should be considered "tainted" for future
471 // process reuse decisions:
472 // (1) If |site_| required a dedicated process, this SiteInstance's process
473 // can only host URLs for the same site.
474 // (2) Even if |site_| does not require a dedicated process, this
475 // SiteInstance's process still cannot be reused to host other sites
476 // requiring dedicated sites in the future.
477 // We can get here either when we commit a URL into a SiteInstance that does
478 // not yet have a site, or when we create a process for a SiteInstance with a
479 // preassigned site.
480 process_->SetIsUsed();
481
nickd30fd962015-07-27 21:51:08482 // TODO(nick): When all sites are isolated, this operation provides strong
483 // protection. If only some sites are isolated, we need additional logic to
484 // prevent the non-isolated sites from requesting resources for isolated
485 // sites. https://crbug.com/509125
alexmos13fe1962017-06-28 04:25:12486 if (ShouldLockToOrigin(GetBrowserContext(), site_)) {
[email protected]b9535422012-02-09 01:47:59487 ChildProcessSecurityPolicyImpl* policy =
488 ChildProcessSecurityPolicyImpl::GetInstance();
[email protected]313b80bd2011-11-23 03:49:10489 policy->LockToOrigin(process_->GetID(), site_);
490 }
491}
[email protected]46488322012-10-30 03:22:20492
493} // namespace content