blob: 987dd5c9d16693aea23add3c8b269ffe623054f8 [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
[email protected]313b80bd2011-11-23 03:49:107#include "base/command_line.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"
[email protected]f3b1a082011-11-18 00:34:3011#include "content/browser/renderer_host/render_process_host_impl.h"
[email protected]4c3a23582012-08-18 08:54:3412#include "content/browser/storage_partition_impl.h"
[email protected]87f3c082011-10-19 18:07:4413#include "content/public/browser/content_browser_client.h"
[email protected]f3b1a082011-11-18 00:34:3014#include "content/public/browser/render_process_host_factory.h"
[email protected]41fb79a52012-06-29 16:34:3315#include "content/public/browser/web_ui_controller_factory.h"
[email protected]313b80bd2011-11-23 03:49:1016#include "content/public/common/content_switches.h"
[email protected]a1d29162011-10-14 17:14:0317#include "content/public/common/url_constants.h"
[email protected]be28b5f42012-07-20 11:31:2518#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
initial.commit09911bf2008-07-26 23:55:2919
[email protected]46488322012-10-30 03:22:2020namespace content {
[email protected]b6583592012-01-25 19:52:3321
[email protected]3059caa2013-06-06 03:48:4122const RenderProcessHostFactory*
23 SiteInstanceImpl::g_render_process_host_factory_ = NULL;
[email protected]b6583592012-01-25 19:52:3324int32 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_++),
[email protected]cee12ab2013-07-12 01:53:0728 active_view_count_(0),
[email protected]992db4c2011-05-12 15:37:1529 browsing_instance_(browsing_instance),
[email protected]4566f132009-03-12 01:55:1330 process_(NULL),
[email protected]4566f132009-03-12 01:55:1331 has_site_(false) {
32 DCHECK(browsing_instance);
[email protected]4566f132009-03-12 01:55:1333}
34
[email protected]b6583592012-01-25 19:52:3335SiteInstanceImpl::~SiteInstanceImpl() {
[email protected]46488322012-10-30 03:22:2036 GetContentClient()->browser()->SiteInstanceDeleting(this);
[email protected]056ad2a2011-07-12 02:13:5537
[email protected]40bfaf32013-11-19 01:35:4338 if (process_)
39 process_->RemoveObserver(this);
40
initial.commit09911bf2008-07-26 23:55:2941 // Now that no one is referencing us, we can safely remove ourselves from
42 // the BrowsingInstance. Any future visits to a page from this site
43 // (within the same BrowsingInstance) can safely create a new SiteInstance.
44 if (has_site_)
[email protected]b6583592012-01-25 19:52:3345 browsing_instance_->UnregisterSiteInstance(
46 static_cast<SiteInstance*>(this));
initial.commit09911bf2008-07-26 23:55:2947}
48
[email protected]b6583592012-01-25 19:52:3349int32 SiteInstanceImpl::GetId() {
50 return id_;
51}
52
53bool SiteInstanceImpl::HasProcess() const {
[email protected]41fb79a52012-06-29 16:34:3354 if (process_ != NULL)
55 return true;
56
57 // If we would use process-per-site for this site, also check if there is an
58 // existing process that we would use if GetProcess() were called.
[email protected]46488322012-10-30 03:22:2059 BrowserContext* browser_context =
[email protected]41fb79a52012-06-29 16:34:3360 browsing_instance_->browser_context();
61 if (has_site_ &&
[email protected]1ae93fb12013-06-14 03:38:5662 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_) &&
[email protected]41fb79a52012-06-29 16:34:3363 RenderProcessHostImpl::GetProcessHostForSite(browser_context, site_)) {
64 return true;
65 }
66
67 return false;
[email protected]5ab79b02010-04-26 16:47:1168}
69
[email protected]41fb79a52012-06-29 16:34:3370RenderProcessHost* SiteInstanceImpl::GetProcess() {
[email protected]08c8ec7e2010-07-11 17:14:4871 // TODO(erikkay) It would be nice to ensure that the renderer type had been
72 // properly set before we get here. The default tab creation case winds up
73 // with no site set at this point, so it will default to TYPE_NORMAL. This
74 // may not be correct, so we'll wind up potentially creating a process that
75 // we then throw away, or worse sharing a process with the wrong process type.
76 // See crbug.com/43448.
77
initial.commit09911bf2008-07-26 23:55:2978 // Create a new process if ours went away or was reused.
[email protected]4566f132009-03-12 01:55:1379 if (!process_) {
[email protected]4c3a23582012-08-18 08:54:3480 BrowserContext* browser_context = browsing_instance_->browser_context();
[email protected]41fb79a52012-06-29 16:34:3381
82 // If we should use process-per-site mode (either in general or for the
83 // given site), then look for an existing RenderProcessHost for the site.
84 bool use_process_per_site = has_site_ &&
[email protected]1ae93fb12013-06-14 03:38:5685 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_);
[email protected]41fb79a52012-06-29 16:34:3386 if (use_process_per_site) {
87 process_ = RenderProcessHostImpl::GetProcessHostForSite(browser_context,
88 site_);
89 }
90
91 // If not (or if none found), see if we should reuse an existing process.
92 if (!process_ && RenderProcessHostImpl::ShouldTryToUseExistingProcessHost(
93 browser_context, site_)) {
94 process_ = RenderProcessHostImpl::GetExistingProcessHost(browser_context,
95 site_);
96 }
initial.commit09911bf2008-07-26 23:55:2997
98 // Otherwise (or if that fails), create a new one.
[email protected]4566f132009-03-12 01:55:1399 if (!process_) {
[email protected]3059caa2013-06-06 03:48:41100 if (g_render_process_host_factory_) {
101 process_ = g_render_process_host_factory_->CreateRenderProcessHost(
[email protected]f681b072013-06-06 21:53:12102 browser_context, this);
[email protected]a6df5112009-01-21 23:50:15103 } else {
[email protected]4c3a23582012-08-18 08:54:34104 StoragePartitionImpl* partition =
105 static_cast<StoragePartitionImpl*>(
106 BrowserContext::GetStoragePartition(browser_context, this));
[email protected]6eb1a11e2013-10-09 00:54:37107 process_ = new RenderProcessHostImpl(browser_context,
108 partition,
[email protected]6eb1a11e2013-10-09 00:54:37109 site_.SchemeIs(kGuestScheme));
[email protected]a6df5112009-01-21 23:50:15110 }
111 }
[email protected]41fb79a52012-06-29 16:34:33112 CHECK(process_);
[email protected]40bfaf32013-11-19 01:35:43113 process_->AddObserver(this);
[email protected]41fb79a52012-06-29 16:34:33114
115 // If we are using process-per-site, we need to register this process
116 // for the current site so that we can find it again. (If no site is set
117 // at this time, we will register it in SetSite().)
118 if (use_process_per_site) {
119 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_)
128 LockToOrigin();
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
[email protected]41fb79a52012-06-29 16:34:33157 if (process_) {
[email protected]313b80bd2011-11-23 03:49:10158 LockToOrigin();
[email protected]41fb79a52012-06-29 16:34:33159
160 // Ensure the process is registered for this site if necessary.
[email protected]1ae93fb12013-06-14 03:38:56161 if (RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_)) {
[email protected]41fb79a52012-06-29 16:34:33162 RenderProcessHostImpl::RegisterProcessHostForSite(
163 browser_context, process_, site_);
164 }
165 }
initial.commit09911bf2008-07-26 23:55:29166}
167
[email protected]77ab17312012-09-28 15:34:59168const GURL& SiteInstanceImpl::GetSiteURL() const {
[email protected]b6583592012-01-25 19:52:33169 return site_;
170}
171
172bool SiteInstanceImpl::HasSite() const {
173 return has_site_;
174}
175
176bool SiteInstanceImpl::HasRelatedSiteInstance(const GURL& url) {
initial.commit09911bf2008-07-26 23:55:29177 return browsing_instance_->HasSiteInstance(url);
178}
179
[email protected]b6583592012-01-25 19:52:33180SiteInstance* SiteInstanceImpl::GetRelatedSiteInstance(const GURL& url) {
initial.commit09911bf2008-07-26 23:55:29181 return browsing_instance_->GetSiteInstanceForURL(url);
182}
183
[email protected]14392a52012-05-02 20:28:44184bool SiteInstanceImpl::IsRelatedSiteInstance(const SiteInstance* instance) {
[email protected]fc72bb12013-06-02 21:13:46185 return browsing_instance_.get() == static_cast<const SiteInstanceImpl*>(
186 instance)->browsing_instance_.get();
[email protected]14392a52012-05-02 20:28:44187}
188
[email protected]d5072a82014-05-15 05:50:18189size_t SiteInstanceImpl::GetRelatedActiveContentsCount() {
190 return browsing_instance_->active_contents_count();
191}
192
[email protected]f88628d02012-11-11 17:58:59193bool SiteInstanceImpl::HasWrongProcessForURL(const GURL& url) {
[email protected]d292d8a2011-05-25 03:47:11194 // Having no process isn't a problem, since we'll assign it correctly.
[email protected]f88628d02012-11-11 17:58:59195 // Note that HasProcess() may return true if process_ is null, in
196 // process-per-site cases where there's an existing process available.
197 // We want to use such a process in the IsSuitableHost check, so we
198 // may end up assigning process_ in the GetProcess() call below.
199 if (!HasProcess())
[email protected]d292d8a2011-05-25 03:47:11200 return false;
201
[email protected]144a8102012-01-14 01:05:31202 // If the URL to navigate to can be associated with any site instance,
203 // we want to keep it in the same process.
[email protected]c02f1ba2014-02-03 06:53:53204 if (IsRendererDebugURL(url))
[email protected]144a8102012-01-14 01:05:31205 return false;
206
[email protected]88aae972011-12-16 01:14:18207 // If the site URL is an extension (e.g., for hosted apps or WebUI) but the
[email protected]d292d8a2011-05-25 03:47:11208 // process is not (or vice versa), make sure we notice and fix it.
[email protected]2a5221b2011-09-27 23:07:31209 GURL site_url = GetSiteForURL(browsing_instance_->browser_context(), url);
[email protected]88aae972011-12-16 01:14:18210 return !RenderProcessHostImpl::IsSuitableHost(
[email protected]f88628d02012-11-11 17:58:59211 GetProcess(), browsing_instance_->browser_context(), site_url);
[email protected]d292d8a2011-05-25 03:47:11212}
213
[email protected]d5072a82014-05-15 05:50:18214void SiteInstanceImpl::IncrementRelatedActiveContentsCount() {
215 browsing_instance_->increment_active_contents_count();
216}
217
218void SiteInstanceImpl::DecrementRelatedActiveContentsCount() {
219 browsing_instance_->decrement_active_contents_count();
220}
221
[email protected]3059caa2013-06-06 03:48:41222void SiteInstanceImpl::set_render_process_host_factory(
223 const RenderProcessHostFactory* rph_factory) {
224 g_render_process_host_factory_ = rph_factory;
225}
226
[email protected]4c3a23582012-08-18 08:54:34227BrowserContext* SiteInstanceImpl::GetBrowserContext() const {
[email protected]72daaa92012-01-18 13:39:02228 return browsing_instance_->browser_context();
229}
230
initial.commit09911bf2008-07-26 23:55:29231/*static*/
[email protected]4c3a23582012-08-18 08:54:34232SiteInstance* SiteInstance::Create(BrowserContext* browser_context) {
[email protected]b6583592012-01-25 19:52:33233 return new SiteInstanceImpl(new BrowsingInstance(browser_context));
initial.commit09911bf2008-07-26 23:55:29234}
235
236/*static*/
[email protected]4c3a23582012-08-18 08:54:34237SiteInstance* SiteInstance::CreateForURL(BrowserContext* browser_context,
238 const GURL& url) {
[email protected]633fbc42009-05-07 23:39:47239 // This BrowsingInstance may be deleted if it returns an existing
240 // SiteInstance.
[email protected]3d7474ff2011-07-27 17:47:37241 scoped_refptr<BrowsingInstance> instance(
242 new BrowsingInstance(browser_context));
[email protected]633fbc42009-05-07 23:39:47243 return instance->GetSiteInstanceForURL(url);
[email protected]d8a96622009-05-07 19:21:16244}
245
246/*static*/
[email protected]399583b2012-12-11 09:33:42247bool SiteInstance::IsSameWebSite(BrowserContext* browser_context,
[email protected]855d7d572014-08-02 11:18:17248 const GURL& real_src_url,
249 const GURL& real_dest_url) {
250 GURL src_url = SiteInstanceImpl::GetEffectiveURL(browser_context,
251 real_src_url);
252 GURL dest_url = SiteInstanceImpl::GetEffectiveURL(browser_context,
253 real_dest_url);
[email protected]399583b2012-12-11 09:33:42254
255 // We infer web site boundaries based on the registered domain name of the
256 // top-level page and the scheme. We do not pay attention to the port if
257 // one is present, because pages served from different ports can still
258 // access each other if they change their document.domain variable.
259
260 // Some special URLs will match the site instance of any other URL. This is
261 // done before checking both of them for validity, since we want these URLs
262 // to have the same site instance as even an invalid one.
[email protected]855d7d572014-08-02 11:18:17263 if (IsRendererDebugURL(src_url) || IsRendererDebugURL(dest_url))
[email protected]399583b2012-12-11 09:33:42264 return true;
265
266 // If either URL is invalid, they aren't part of the same site.
[email protected]855d7d572014-08-02 11:18:17267 if (!src_url.is_valid() || !dest_url.is_valid())
[email protected]399583b2012-12-11 09:33:42268 return false;
269
[email protected]855d7d572014-08-02 11:18:17270 // If the destination url is just a blank page, we treat them as part of the
271 // same site.
272 GURL blank_page(url::kAboutBlankURL);
273 if (dest_url == blank_page)
274 return true;
275
[email protected]399583b2012-12-11 09:33:42276 // If the schemes differ, they aren't part of the same site.
[email protected]855d7d572014-08-02 11:18:17277 if (src_url.scheme() != dest_url.scheme())
[email protected]399583b2012-12-11 09:33:42278 return false;
279
[email protected]ed32c212013-05-14 20:49:29280 return net::registry_controlled_domains::SameDomainOrHost(
[email protected]855d7d572014-08-02 11:18:17281 src_url,
282 dest_url,
[email protected]aabe1792014-01-30 21:37:46283 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
[email protected]399583b2012-12-11 09:33:42284}
285
286/*static*/
287GURL SiteInstance::GetSiteForURL(BrowserContext* browser_context,
288 const GURL& real_url) {
[email protected]25a9e8e532012-06-22 06:16:28289 // TODO(fsamuel, creis): For some reason appID is not recognized as a host.
[email protected]6eb1a11e2013-10-09 00:54:37290 if (real_url.SchemeIs(kGuestScheme))
[email protected]25a9e8e532012-06-22 06:16:28291 return real_url;
292
[email protected]b6583592012-01-25 19:52:33293 GURL url = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url);
[email protected]3a8eecb2010-04-22 23:56:30294
initial.commit09911bf2008-07-26 23:55:29295 // URLs with no host should have an empty site.
296 GURL site;
297
298 // TODO(creis): For many protocols, we should just treat the scheme as the
[email protected]76a010b2008-12-07 23:48:03299 // site, since there is no host. e.g., file:, about:, chrome:
initial.commit09911bf2008-07-26 23:55:29300
301 // If the url has a host, then determine the site.
302 if (url.has_host()) {
[email protected]6705b232008-11-26 00:16:51303 // Only keep the scheme and registered domain as given by GetOrigin. This
304 // may also include a port, which we need to drop.
initial.commit09911bf2008-07-26 23:55:29305 site = url.GetOrigin();
306
[email protected]6705b232008-11-26 00:16:51307 // Remove port, if any.
308 if (site.has_port()) {
309 GURL::Replacements rep;
310 rep.ClearPort();
311 site = site.ReplaceComponents(rep);
312 }
313
initial.commit09911bf2008-07-26 23:55:29314 // If this URL has a registered domain, we only want to remember that part.
315 std::string domain =
[email protected]ed32c212013-05-14 20:49:29316 net::registry_controlled_domains::GetDomainAndRegistry(
317 url,
[email protected]aabe1792014-01-30 21:37:46318 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
initial.commit09911bf2008-07-26 23:55:29319 if (!domain.empty()) {
320 GURL::Replacements rep;
321 rep.SetHostStr(domain);
322 site = site.ReplaceComponents(rep);
323 }
324 }
325 return site;
326}
327
328/*static*/
[email protected]4c3a23582012-08-18 08:54:34329GURL SiteInstanceImpl::GetEffectiveURL(BrowserContext* browser_context,
330 const GURL& url) {
[email protected]46488322012-10-30 03:22:20331 return GetContentClient()->browser()->
[email protected]3d7474ff2011-07-27 17:47:37332 GetEffectiveURL(browser_context, url);
[email protected]3a8eecb2010-04-22 23:56:30333}
334
[email protected]40bfaf32013-11-19 01:35:43335void SiteInstanceImpl::RenderProcessHostDestroyed(RenderProcessHost* host) {
336 DCHECK_EQ(process_, host);
337 process_->RemoveObserver(this);
338 process_ = NULL;
[email protected]4566f132009-03-12 01:55:13339}
[email protected]313b80bd2011-11-23 03:49:10340
[email protected]b6583592012-01-25 19:52:33341void SiteInstanceImpl::LockToOrigin() {
[email protected]c6f2e672012-11-15 01:47:02342 // We currently only restrict this process to a particular site if the
343 // --enable-strict-site-isolation or --site-per-process flags are present.
[email protected]479278702014-08-11 20:32:09344 const base::CommandLine& command_line =
345 *base::CommandLine::ForCurrentProcess();
[email protected]c6f2e672012-11-15 01:47:02346 if (command_line.HasSwitch(switches::kEnableStrictSiteIsolation) ||
347 command_line.HasSwitch(switches::kSitePerProcess)) {
[email protected]b9535422012-02-09 01:47:59348 ChildProcessSecurityPolicyImpl* policy =
349 ChildProcessSecurityPolicyImpl::GetInstance();
[email protected]313b80bd2011-11-23 03:49:10350 policy->LockToOrigin(process_->GetID(), site_);
351 }
352}
[email protected]46488322012-10-30 03:22:20353
354} // namespace content