blob: 0dd62877d2ec961eacf4adb5fd4d41bd92ba66df [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
avicb129c02017-05-03 06:49:297#include "base/memory/ptr_util.h"
[email protected]39365212011-02-24 01:01:008#include "content/browser/browsing_instance.h"
[email protected]b9535422012-02-09 01:47:599#include "content/browser/child_process_security_policy_impl.h"
[email protected]c02f1ba2014-02-03 06:53:5310#include "content/browser/frame_host/debug_urls.h"
nick9f34e2892016-01-12 21:01:0711#include "content/browser/frame_host/frame_tree_node.h"
[email protected]f3b1a082011-11-18 00:34:3012#include "content/browser/renderer_host/render_process_host_impl.h"
[email protected]4c3a23582012-08-18 08:54:3413#include "content/browser/storage_partition_impl.h"
nickd30fd962015-07-27 21:51:0814#include "content/common/site_isolation_policy.h"
[email protected]87f3c082011-10-19 18:07:4415#include "content/public/browser/content_browser_client.h"
[email protected]f3b1a082011-11-18 00:34:3016#include "content/public/browser/render_process_host_factory.h"
[email protected]41fb79a52012-06-29 16:34:3317#include "content/public/browser/web_ui_controller_factory.h"
[email protected]a1d29162011-10-14 17:14:0318#include "content/public/common/url_constants.h"
[email protected]be28b5f42012-07-20 11:31:2519#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
initial.commit09911bf2008-07-26 23:55:2920
[email protected]46488322012-10-30 03:22:2021namespace content {
[email protected]b6583592012-01-25 19:52:3322
[email protected]3059caa2013-06-06 03:48:4123const RenderProcessHostFactory*
24 SiteInstanceImpl::g_render_process_host_factory_ = NULL;
avib7348942015-12-25 20:57:1025int32_t SiteInstanceImpl::next_site_instance_id_ = 1;
[email protected]992db4c2011-05-12 15:37:1526
[email protected]b6583592012-01-25 19:52:3327SiteInstanceImpl::SiteInstanceImpl(BrowsingInstance* browsing_instance)
[email protected]992db4c2011-05-12 15:37:1528 : id_(next_site_instance_id_++),
creiscce56cd2014-09-29 22:45:2229 active_frame_count_(0),
[email protected]992db4c2011-05-12 15:37:1530 browsing_instance_(browsing_instance),
nickd5bbd0b2016-03-31 19:52:4431 process_(nullptr),
32 has_site_(false),
33 is_default_subframe_site_instance_(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
avi6d686db2017-01-06 02:28:5786namespace {
87
88const void* const kDefaultSubframeProcessHostHolderKey =
89 &kDefaultSubframeProcessHostHolderKey;
90
91class DefaultSubframeProcessHostHolder : public base::SupportsUserData::Data,
92 public RenderProcessHostObserver {
93 public:
94 explicit DefaultSubframeProcessHostHolder(BrowserContext* browser_context)
95 : browser_context_(browser_context) {}
96 ~DefaultSubframeProcessHostHolder() override {}
97
98 // Gets the correct render process to use for this SiteInstance.
99 RenderProcessHost* GetProcessHost(SiteInstance* site_instance,
100 bool is_for_guests_only) {
101 StoragePartition* default_partition =
102 BrowserContext::GetDefaultStoragePartition(browser_context_);
103 StoragePartition* partition =
104 BrowserContext::GetStoragePartition(browser_context_, site_instance);
105
106 // Is this the default storage partition? If it isn't, then just give it its
107 // own non-shared process.
108 if (partition != default_partition || is_for_guests_only) {
109 RenderProcessHostImpl* host = new RenderProcessHostImpl(
110 browser_context_, static_cast<StoragePartitionImpl*>(partition),
111 is_for_guests_only);
112 host->SetIsNeverSuitableForReuse();
113 return host;
114 }
115
116 if (host_) {
117 // If we already have a shared host for the default storage partition, use
118 // it.
119 return host_;
120 }
121
122 host_ = new RenderProcessHostImpl(
123 browser_context_, static_cast<StoragePartitionImpl*>(partition),
124 false /* for guests only */);
125 host_->SetIsNeverSuitableForReuse();
126 host_->AddObserver(this);
127
128 return host_;
129 }
130
131 void RenderProcessHostDestroyed(RenderProcessHost* host) override {
132 DCHECK_EQ(host_, host);
133 host_->RemoveObserver(this);
134 host_ = nullptr;
135 }
136
137 private:
138 BrowserContext* browser_context_;
139
140 // The default subframe render process used for the default storage partition
141 // of this BrowserContext.
142 RenderProcessHostImpl* host_ = nullptr;
143};
144
145} // namespace
146
147RenderProcessHost* SiteInstanceImpl::GetDefaultSubframeProcessHost(
148 BrowserContext* browser_context,
149 bool is_for_guests_only) {
150 DefaultSubframeProcessHostHolder* holder =
151 static_cast<DefaultSubframeProcessHostHolder*>(
152 browser_context->GetUserData(&kDefaultSubframeProcessHostHolderKey));
153 if (!holder) {
154 holder = new DefaultSubframeProcessHostHolder(browser_context);
avicb129c02017-05-03 06:49:29155 browser_context->SetUserData(kDefaultSubframeProcessHostHolderKey,
156 base::WrapUnique(holder));
avi6d686db2017-01-06 02:28:57157 }
158
159 return holder->GetProcessHost(this, is_for_guests_only);
160}
161
[email protected]41fb79a52012-06-29 16:34:33162RenderProcessHost* SiteInstanceImpl::GetProcess() {
[email protected]08c8ec7e2010-07-11 17:14:48163 // TODO(erikkay) It would be nice to ensure that the renderer type had been
164 // properly set before we get here. The default tab creation case winds up
165 // with no site set at this point, so it will default to TYPE_NORMAL. This
166 // may not be correct, so we'll wind up potentially creating a process that
167 // we then throw away, or worse sharing a process with the wrong process type.
168 // See crbug.com/43448.
169
initial.commit09911bf2008-07-26 23:55:29170 // Create a new process if ours went away or was reused.
[email protected]4566f132009-03-12 01:55:13171 if (!process_) {
[email protected]4c3a23582012-08-18 08:54:34172 BrowserContext* browser_context = browsing_instance_->browser_context();
avi6d686db2017-01-06 02:28:57173 bool is_for_guests_only = site_.SchemeIs(kGuestScheme);
[email protected]41fb79a52012-06-29 16:34:33174
175 // If we should use process-per-site mode (either in general or for the
176 // given site), then look for an existing RenderProcessHost for the site.
177 bool use_process_per_site = has_site_ &&
[email protected]1ae93fb12013-06-14 03:38:56178 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_);
[email protected]41fb79a52012-06-29 16:34:33179 if (use_process_per_site) {
180 process_ = RenderProcessHostImpl::GetProcessHostForSite(browser_context,
181 site_);
182 }
183
avi6d686db2017-01-06 02:28:57184 if (!process_ && IsDefaultSubframeSiteInstance() &&
185 SiteIsolationPolicy::IsTopDocumentIsolationEnabled()) {
186 process_ =
187 GetDefaultSubframeProcessHost(browser_context, is_for_guests_only);
188 }
189
[email protected]41fb79a52012-06-29 16:34:33190 // If not (or if none found), see if we should reuse an existing process.
191 if (!process_ && RenderProcessHostImpl::ShouldTryToUseExistingProcessHost(
192 browser_context, site_)) {
193 process_ = RenderProcessHostImpl::GetExistingProcessHost(browser_context,
194 site_);
195 }
initial.commit09911bf2008-07-26 23:55:29196
197 // Otherwise (or if that fails), create a new one.
[email protected]4566f132009-03-12 01:55:13198 if (!process_) {
[email protected]3059caa2013-06-06 03:48:41199 if (g_render_process_host_factory_) {
200 process_ = g_render_process_host_factory_->CreateRenderProcessHost(
[email protected]f681b072013-06-06 21:53:12201 browser_context, this);
[email protected]a6df5112009-01-21 23:50:15202 } else {
[email protected]4c3a23582012-08-18 08:54:34203 StoragePartitionImpl* partition =
204 static_cast<StoragePartitionImpl*>(
205 BrowserContext::GetStoragePartition(browser_context, this));
avi6d686db2017-01-06 02:28:57206 process_ = new RenderProcessHostImpl(browser_context, partition,
207 is_for_guests_only);
[email protected]a6df5112009-01-21 23:50:15208 }
209 }
[email protected]41fb79a52012-06-29 16:34:33210 CHECK(process_);
[email protected]40bfaf32013-11-19 01:35:43211 process_->AddObserver(this);
[email protected]41fb79a52012-06-29 16:34:33212
213 // If we are using process-per-site, we need to register this process
214 // for the current site so that we can find it again. (If no site is set
215 // at this time, we will register it in SetSite().)
216 if (use_process_per_site) {
217 RenderProcessHostImpl::RegisterProcessHostForSite(browser_context,
218 process_, site_);
219 }
initial.commit09911bf2008-07-26 23:55:29220
naskob8744d22014-08-28 17:07:43221 TRACE_EVENT2("navigation", "SiteInstanceImpl::GetProcess",
222 "site id", id_, "process id", process_->GetID());
[email protected]46488322012-10-30 03:22:20223 GetContentClient()->browser()->SiteInstanceGotProcess(this);
[email protected]6f371442011-11-09 06:45:46224
[email protected]313b80bd2011-11-23 03:49:10225 if (has_site_)
226 LockToOrigin();
initial.commit09911bf2008-07-26 23:55:29227 }
[email protected]4566f132009-03-12 01:55:13228 DCHECK(process_);
initial.commit09911bf2008-07-26 23:55:29229
[email protected]4566f132009-03-12 01:55:13230 return process_;
initial.commit09911bf2008-07-26 23:55:29231}
232
[email protected]b6583592012-01-25 19:52:33233void SiteInstanceImpl::SetSite(const GURL& url) {
naskob8744d22014-08-28 17:07:43234 TRACE_EVENT2("navigation", "SiteInstanceImpl::SetSite",
235 "site id", id_, "url", url.possibly_invalid_spec());
initial.commit09911bf2008-07-26 23:55:29236 // A SiteInstance's site should not change.
237 // TODO(creis): When following links or script navigations, we can currently
238 // render pages from other sites in this SiteInstance. This will eventually
239 // be fixed, but until then, we should still not set the site of a
240 // SiteInstance more than once.
241 DCHECK(!has_site_);
242
243 // Remember that this SiteInstance has been used to load a URL, even if the
244 // URL is invalid.
245 has_site_ = true;
[email protected]4c3a23582012-08-18 08:54:34246 BrowserContext* browser_context = browsing_instance_->browser_context();
[email protected]41fb79a52012-06-29 16:34:33247 site_ = GetSiteForURL(browser_context, url);
initial.commit09911bf2008-07-26 23:55:29248
249 // Now that we have a site, register it with the BrowsingInstance. This
250 // ensures that we won't create another SiteInstance for this site within
251 // the same BrowsingInstance, because all same-site pages within a
252 // BrowsingInstance can script each other.
253 browsing_instance_->RegisterSiteInstance(this);
[email protected]313b80bd2011-11-23 03:49:10254
[email protected]41fb79a52012-06-29 16:34:33255 if (process_) {
[email protected]313b80bd2011-11-23 03:49:10256 LockToOrigin();
[email protected]41fb79a52012-06-29 16:34:33257
258 // Ensure the process is registered for this site if necessary.
[email protected]1ae93fb12013-06-14 03:38:56259 if (RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_)) {
[email protected]41fb79a52012-06-29 16:34:33260 RenderProcessHostImpl::RegisterProcessHostForSite(
261 browser_context, process_, site_);
262 }
263 }
initial.commit09911bf2008-07-26 23:55:29264}
265
[email protected]77ab17312012-09-28 15:34:59266const GURL& SiteInstanceImpl::GetSiteURL() const {
[email protected]b6583592012-01-25 19:52:33267 return site_;
268}
269
270bool SiteInstanceImpl::HasSite() const {
271 return has_site_;
272}
273
274bool SiteInstanceImpl::HasRelatedSiteInstance(const GURL& url) {
initial.commit09911bf2008-07-26 23:55:29275 return browsing_instance_->HasSiteInstance(url);
276}
277
dchengbccd6b82016-03-30 16:24:19278scoped_refptr<SiteInstance> SiteInstanceImpl::GetRelatedSiteInstance(
279 const GURL& url) {
initial.commit09911bf2008-07-26 23:55:29280 return browsing_instance_->GetSiteInstanceForURL(url);
281}
282
[email protected]14392a52012-05-02 20:28:44283bool SiteInstanceImpl::IsRelatedSiteInstance(const SiteInstance* instance) {
[email protected]fc72bb12013-06-02 21:13:46284 return browsing_instance_.get() == static_cast<const SiteInstanceImpl*>(
285 instance)->browsing_instance_.get();
[email protected]14392a52012-05-02 20:28:44286}
287
[email protected]d5072a82014-05-15 05:50:18288size_t SiteInstanceImpl::GetRelatedActiveContentsCount() {
289 return browsing_instance_->active_contents_count();
290}
291
[email protected]f88628d02012-11-11 17:58:59292bool SiteInstanceImpl::HasWrongProcessForURL(const GURL& url) {
[email protected]d292d8a2011-05-25 03:47:11293 // Having no process isn't a problem, since we'll assign it correctly.
[email protected]f88628d02012-11-11 17:58:59294 // Note that HasProcess() may return true if process_ is null, in
295 // process-per-site cases where there's an existing process available.
296 // We want to use such a process in the IsSuitableHost check, so we
297 // may end up assigning process_ in the GetProcess() call below.
298 if (!HasProcess())
[email protected]d292d8a2011-05-25 03:47:11299 return false;
300
[email protected]144a8102012-01-14 01:05:31301 // If the URL to navigate to can be associated with any site instance,
302 // we want to keep it in the same process.
[email protected]c02f1ba2014-02-03 06:53:53303 if (IsRendererDebugURL(url))
[email protected]144a8102012-01-14 01:05:31304 return false;
305
[email protected]88aae972011-12-16 01:14:18306 // If the site URL is an extension (e.g., for hosted apps or WebUI) but the
[email protected]d292d8a2011-05-25 03:47:11307 // process is not (or vice versa), make sure we notice and fix it.
[email protected]2a5221b2011-09-27 23:07:31308 GURL site_url = GetSiteForURL(browsing_instance_->browser_context(), url);
[email protected]88aae972011-12-16 01:14:18309 return !RenderProcessHostImpl::IsSuitableHost(
[email protected]f88628d02012-11-11 17:58:59310 GetProcess(), browsing_instance_->browser_context(), site_url);
[email protected]d292d8a2011-05-25 03:47:11311}
312
nickd5bbd0b2016-03-31 19:52:44313scoped_refptr<SiteInstanceImpl>
314SiteInstanceImpl::GetDefaultSubframeSiteInstance() {
315 return browsing_instance_->GetDefaultSubframeSiteInstance();
316}
317
nickd30fd962015-07-27 21:51:08318bool SiteInstanceImpl::RequiresDedicatedProcess() {
319 if (!has_site_)
320 return false;
nickd5bbd0b2016-03-31 19:52:44321
322 return DoesSiteRequireDedicatedProcess(GetBrowserContext(), site_);
nickd30fd962015-07-27 21:51:08323}
324
avi85cacb72016-10-26 22:39:33325bool SiteInstanceImpl::IsDefaultSubframeSiteInstance() const {
326 return is_default_subframe_site_instance_;
327}
328
nick9f34e2892016-01-12 21:01:07329void SiteInstanceImpl::IncrementActiveFrameCount() {
330 active_frame_count_++;
331}
332
333void SiteInstanceImpl::DecrementActiveFrameCount() {
ericwilligers254597b2016-10-17 10:32:31334 if (--active_frame_count_ == 0) {
335 for (auto& observer : observers_)
336 observer.ActiveFrameCountIsZero(this);
337 }
nick9f34e2892016-01-12 21:01:07338}
339
[email protected]d5072a82014-05-15 05:50:18340void SiteInstanceImpl::IncrementRelatedActiveContentsCount() {
341 browsing_instance_->increment_active_contents_count();
342}
343
344void SiteInstanceImpl::DecrementRelatedActiveContentsCount() {
345 browsing_instance_->decrement_active_contents_count();
346}
347
nick9f34e2892016-01-12 21:01:07348void SiteInstanceImpl::AddObserver(Observer* observer) {
349 observers_.AddObserver(observer);
350}
351
352void SiteInstanceImpl::RemoveObserver(Observer* observer) {
353 observers_.RemoveObserver(observer);
354}
355
[email protected]3059caa2013-06-06 03:48:41356void SiteInstanceImpl::set_render_process_host_factory(
357 const RenderProcessHostFactory* rph_factory) {
358 g_render_process_host_factory_ = rph_factory;
359}
360
[email protected]4c3a23582012-08-18 08:54:34361BrowserContext* SiteInstanceImpl::GetBrowserContext() const {
[email protected]72daaa92012-01-18 13:39:02362 return browsing_instance_->browser_context();
363}
364
nickcc0d9142015-10-14 16:27:10365// static
dchengbccd6b82016-03-30 16:24:19366scoped_refptr<SiteInstance> SiteInstance::Create(
367 BrowserContext* browser_context) {
368 return SiteInstanceImpl::Create(browser_context);
initial.commit09911bf2008-07-26 23:55:29369}
370
nickcc0d9142015-10-14 16:27:10371// static
dchengbccd6b82016-03-30 16:24:19372scoped_refptr<SiteInstance> SiteInstance::CreateForURL(
373 BrowserContext* browser_context,
374 const GURL& url) {
375 return SiteInstanceImpl::CreateForURL(browser_context, url);
[email protected]d8a96622009-05-07 19:21:16376}
377
nickcc0d9142015-10-14 16:27:10378// static
[email protected]399583b2012-12-11 09:33:42379bool SiteInstance::IsSameWebSite(BrowserContext* browser_context,
[email protected]855d7d572014-08-02 11:18:17380 const GURL& real_src_url,
381 const GURL& real_dest_url) {
382 GURL src_url = SiteInstanceImpl::GetEffectiveURL(browser_context,
383 real_src_url);
384 GURL dest_url = SiteInstanceImpl::GetEffectiveURL(browser_context,
385 real_dest_url);
[email protected]399583b2012-12-11 09:33:42386
387 // We infer web site boundaries based on the registered domain name of the
388 // top-level page and the scheme. We do not pay attention to the port if
389 // one is present, because pages served from different ports can still
390 // access each other if they change their document.domain variable.
391
392 // Some special URLs will match the site instance of any other URL. This is
393 // done before checking both of them for validity, since we want these URLs
394 // to have the same site instance as even an invalid one.
[email protected]855d7d572014-08-02 11:18:17395 if (IsRendererDebugURL(src_url) || IsRendererDebugURL(dest_url))
[email protected]399583b2012-12-11 09:33:42396 return true;
397
398 // If either URL is invalid, they aren't part of the same site.
[email protected]855d7d572014-08-02 11:18:17399 if (!src_url.is_valid() || !dest_url.is_valid())
[email protected]399583b2012-12-11 09:33:42400 return false;
401
[email protected]855d7d572014-08-02 11:18:17402 // If the destination url is just a blank page, we treat them as part of the
403 // same site.
404 GURL blank_page(url::kAboutBlankURL);
405 if (dest_url == blank_page)
406 return true;
407
[email protected]399583b2012-12-11 09:33:42408 // If the schemes differ, they aren't part of the same site.
[email protected]855d7d572014-08-02 11:18:17409 if (src_url.scheme() != dest_url.scheme())
[email protected]399583b2012-12-11 09:33:42410 return false;
411
[email protected]ed32c212013-05-14 20:49:29412 return net::registry_controlled_domains::SameDomainOrHost(
[email protected]855d7d572014-08-02 11:18:17413 src_url,
414 dest_url,
[email protected]aabe1792014-01-30 21:37:46415 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
[email protected]399583b2012-12-11 09:33:42416}
417
nickcc0d9142015-10-14 16:27:10418// static
[email protected]399583b2012-12-11 09:33:42419GURL SiteInstance::GetSiteForURL(BrowserContext* browser_context,
420 const GURL& real_url) {
[email protected]25a9e8e532012-06-22 06:16:28421 // TODO(fsamuel, creis): For some reason appID is not recognized as a host.
[email protected]6eb1a11e2013-10-09 00:54:37422 if (real_url.SchemeIs(kGuestScheme))
[email protected]25a9e8e532012-06-22 06:16:28423 return real_url;
424
[email protected]b6583592012-01-25 19:52:33425 GURL url = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url);
nick1dd47922016-04-29 16:44:48426 url::Origin origin(url);
[email protected]3a8eecb2010-04-22 23:56:30427
initial.commit09911bf2008-07-26 23:55:29428 // If the url has a host, then determine the site.
nick1dd47922016-04-29 16:44:48429 if (!origin.host().empty()) {
430 // Only keep the scheme and registered domain of |origin|.
431 std::string domain = net::registry_controlled_domains::GetDomainAndRegistry(
432 origin.host(),
433 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
434 std::string site = origin.scheme();
435 site += url::kStandardSchemeSeparator;
436 site += domain.empty() ? origin.host() : domain;
437 return GURL(site);
initial.commit09911bf2008-07-26 23:55:29438 }
creisf60c2cd2014-12-18 00:41:02439
440 // If there is no host but there is a scheme, return the scheme.
441 // This is useful for cases like file URLs.
442 if (url.has_scheme())
443 return GURL(url.scheme() + ":");
444
445 // Otherwise the URL should be invalid; return an empty site.
446 DCHECK(!url.is_valid());
447 return GURL();
initial.commit09911bf2008-07-26 23:55:29448}
449
nickcc0d9142015-10-14 16:27:10450// static
[email protected]4c3a23582012-08-18 08:54:34451GURL SiteInstanceImpl::GetEffectiveURL(BrowserContext* browser_context,
452 const GURL& url) {
[email protected]46488322012-10-30 03:22:20453 return GetContentClient()->browser()->
[email protected]3d7474ff2011-07-27 17:47:37454 GetEffectiveURL(browser_context, url);
[email protected]3a8eecb2010-04-22 23:56:30455}
456
nickcc0d9142015-10-14 16:27:10457// static
458bool SiteInstanceImpl::DoesSiteRequireDedicatedProcess(
459 BrowserContext* browser_context,
nickdb193a12016-09-09 23:09:23460 const GURL& url) {
nickcc0d9142015-10-14 16:27:10461 // If --site-per-process is enabled, site isolation is enabled everywhere.
462 if (SiteIsolationPolicy::UseDedicatedProcessesForAllSites())
463 return true;
464
nickdb193a12016-09-09 23:09:23465 // Let the content embedder enable site isolation for specific URLs. Use the
466 // canonical site url for this check, so that schemes with nested origins
467 // (blob and filesystem) work properly.
468 GURL site_url = GetSiteForURL(browser_context, url);
nickcc0d9142015-10-14 16:27:10469 if (GetContentClient()->IsSupplementarySiteIsolationModeEnabled() &&
470 GetContentClient()->browser()->DoesSiteRequireDedicatedProcess(
nickdb193a12016-09-09 23:09:23471 browser_context, site_url)) {
nickcc0d9142015-10-14 16:27:10472 return true;
473 }
474
475 return false;
476}
477
[email protected]40bfaf32013-11-19 01:35:43478void SiteInstanceImpl::RenderProcessHostDestroyed(RenderProcessHost* host) {
479 DCHECK_EQ(process_, host);
480 process_->RemoveObserver(this);
nickd5bbd0b2016-03-31 19:52:44481 process_ = nullptr;
[email protected]4566f132009-03-12 01:55:13482}
[email protected]313b80bd2011-11-23 03:49:10483
nick9f34e2892016-01-12 21:01:07484void SiteInstanceImpl::RenderProcessWillExit(RenderProcessHost* host) {
485 // TODO(nick): http://crbug.com/575400 - RenderProcessWillExit might not serve
486 // any purpose here.
ericwilligers254597b2016-10-17 10:32:31487 for (auto& observer : observers_)
488 observer.RenderProcessGone(this);
nick9f34e2892016-01-12 21:01:07489}
490
491void SiteInstanceImpl::RenderProcessExited(RenderProcessHost* host,
492 base::TerminationStatus status,
493 int exit_code) {
ericwilligers254597b2016-10-17 10:32:31494 for (auto& observer : observers_)
495 observer.RenderProcessGone(this);
nick9f34e2892016-01-12 21:01:07496}
497
[email protected]b6583592012-01-25 19:52:33498void SiteInstanceImpl::LockToOrigin() {
nickd30fd962015-07-27 21:51:08499 // TODO(nick): When all sites are isolated, this operation provides strong
500 // protection. If only some sites are isolated, we need additional logic to
501 // prevent the non-isolated sites from requesting resources for isolated
502 // sites. https://crbug.com/509125
nickcc0d9142015-10-14 16:27:10503 if (RequiresDedicatedProcess()) {
lazyboyf5283a42015-06-24 21:53:35504 // Guest processes cannot be locked to its site because guests always have
505 // a fixed SiteInstance. The site of GURLs a guest loads doesn't match that
506 // SiteInstance. So we skip locking the guest process to the site.
507 // TODO(ncarter): Remove this exclusion once we can make origin lock per
508 // RenderFrame routing id.
509 if (site_.SchemeIs(content::kGuestScheme))
510 return;
511
lfg24d083792015-07-17 20:45:35512 // TODO(creis, nick) https://crbug.com/510588 Chrome UI pages use the same
513 // site (chrome://chrome), so they can't be locked because the site being
514 // loaded doesn't match the SiteInstance.
515 if (site_.SchemeIs(content::kChromeUIScheme))
516 return;
517
creise5d6d1732015-08-25 19:47:06518 // TODO(creis, nick): Until we can handle sites with effective URLs at the
519 // call sites of ChildProcessSecurityPolicy::CanAccessDataForOrigin, we
520 // must give the embedder a chance to exempt some sites to avoid process
521 // kills.
nick7d0984c2015-08-29 00:13:46522 if (!GetContentClient()->browser()->ShouldLockToOrigin(
523 browsing_instance_->browser_context(), site_))
creise5d6d1732015-08-25 19:47:06524 return;
525
[email protected]b9535422012-02-09 01:47:59526 ChildProcessSecurityPolicyImpl* policy =
527 ChildProcessSecurityPolicyImpl::GetInstance();
[email protected]313b80bd2011-11-23 03:49:10528 policy->LockToOrigin(process_->GetID(), site_);
529 }
530}
[email protected]46488322012-10-30 03:22:20531
532} // namespace content