blob: b6f7d3327bf8d51ac923a248994ebea36eed815c [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]f3b1a082011-11-18 00:34:3010#include "content/browser/renderer_host/render_process_host_impl.h"
[email protected]4c3a23582012-08-18 08:54:3411#include "content/browser/storage_partition_impl.h"
[email protected]87f3c082011-10-19 18:07:4412#include "content/public/browser/content_browser_client.h"
[email protected]ad50def52011-10-19 23:17:0713#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1614#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:3015#include "content/public/browser/render_process_host_factory.h"
[email protected]41fb79a52012-06-29 16:34:3316#include "content/public/browser/web_ui_controller_factory.h"
[email protected]313b80bd2011-11-23 03:49:1017#include "content/public/common/content_switches.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]2ee0418e2009-06-26 21:00:4323static bool IsURLSameAsAnySiteInstance(const GURL& url) {
24 if (!url.is_valid())
25 return false;
[email protected]0f012df82011-05-19 14:15:2926
[email protected]89f550b2011-06-08 18:34:0327 // We treat javascript: as the same site as any URL since it is actually
28 // a modifier on existing pages.
29 if (url.SchemeIs(chrome::kJavaScriptScheme))
[email protected]0f012df82011-05-19 14:15:2930 return true;
[email protected]0f012df82011-05-19 14:15:2931
[email protected]f8a6d732013-03-02 22:46:0332 return url == GURL(kChromeUICrashURL) ||
33 url == GURL(kChromeUIKillURL) ||
34 url == GURL(kChromeUIHangURL) ||
[email protected]50f86bc2012-11-01 19:59:1335 url == GURL(kChromeUIShorthangURL);
[email protected]2ee0418e2009-06-26 21:00:4336}
37
[email protected]b6583592012-01-25 19:52:3338int32 SiteInstanceImpl::next_site_instance_id_ = 1;
[email protected]992db4c2011-05-12 15:37:1539
[email protected]b6583592012-01-25 19:52:3340SiteInstanceImpl::SiteInstanceImpl(BrowsingInstance* browsing_instance)
[email protected]992db4c2011-05-12 15:37:1541 : id_(next_site_instance_id_++),
42 browsing_instance_(browsing_instance),
[email protected]4566f132009-03-12 01:55:1343 render_process_host_factory_(NULL),
44 process_(NULL),
[email protected]4566f132009-03-12 01:55:1345 has_site_(false) {
46 DCHECK(browsing_instance);
47
[email protected]46488322012-10-30 03:22:2048 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_TERMINATED,
49 NotificationService::AllBrowserContextsAndSources());
[email protected]4566f132009-03-12 01:55:1350}
51
[email protected]b6583592012-01-25 19:52:3352SiteInstanceImpl::~SiteInstanceImpl() {
[email protected]46488322012-10-30 03:22:2053 GetContentClient()->browser()->SiteInstanceDeleting(this);
[email protected]056ad2a2011-07-12 02:13:5554
initial.commit09911bf2008-07-26 23:55:2955 // Now that no one is referencing us, we can safely remove ourselves from
56 // the BrowsingInstance. Any future visits to a page from this site
57 // (within the same BrowsingInstance) can safely create a new SiteInstance.
58 if (has_site_)
[email protected]b6583592012-01-25 19:52:3359 browsing_instance_->UnregisterSiteInstance(
60 static_cast<SiteInstance*>(this));
initial.commit09911bf2008-07-26 23:55:2961}
62
[email protected]b6583592012-01-25 19:52:3363int32 SiteInstanceImpl::GetId() {
64 return id_;
65}
66
67bool SiteInstanceImpl::HasProcess() const {
[email protected]41fb79a52012-06-29 16:34:3368 if (process_ != NULL)
69 return true;
70
71 // If we would use process-per-site for this site, also check if there is an
72 // existing process that we would use if GetProcess() were called.
[email protected]46488322012-10-30 03:22:2073 BrowserContext* browser_context =
[email protected]41fb79a52012-06-29 16:34:3374 browsing_instance_->browser_context();
75 if (has_site_ &&
76 RenderProcessHostImpl::ShouldUseProcessPerSite(browser_context, site_) &&
77 RenderProcessHostImpl::GetProcessHostForSite(browser_context, site_)) {
78 return true;
79 }
80
81 return false;
[email protected]5ab79b02010-04-26 16:47:1182}
83
[email protected]41fb79a52012-06-29 16:34:3384RenderProcessHost* SiteInstanceImpl::GetProcess() {
[email protected]08c8ec7e2010-07-11 17:14:4885 // TODO(erikkay) It would be nice to ensure that the renderer type had been
86 // properly set before we get here. The default tab creation case winds up
87 // with no site set at this point, so it will default to TYPE_NORMAL. This
88 // may not be correct, so we'll wind up potentially creating a process that
89 // we then throw away, or worse sharing a process with the wrong process type.
90 // See crbug.com/43448.
91
initial.commit09911bf2008-07-26 23:55:2992 // Create a new process if ours went away or was reused.
[email protected]4566f132009-03-12 01:55:1393 if (!process_) {
[email protected]4c3a23582012-08-18 08:54:3494 BrowserContext* browser_context = browsing_instance_->browser_context();
[email protected]41fb79a52012-06-29 16:34:3395
96 // If we should use process-per-site mode (either in general or for the
97 // given site), then look for an existing RenderProcessHost for the site.
98 bool use_process_per_site = has_site_ &&
99 RenderProcessHostImpl::ShouldUseProcessPerSite(browser_context, site_);
100 if (use_process_per_site) {
101 process_ = RenderProcessHostImpl::GetProcessHostForSite(browser_context,
102 site_);
103 }
104
105 // If not (or if none found), see if we should reuse an existing process.
106 if (!process_ && RenderProcessHostImpl::ShouldTryToUseExistingProcessHost(
107 browser_context, site_)) {
108 process_ = RenderProcessHostImpl::GetExistingProcessHost(browser_context,
109 site_);
110 }
initial.commit09911bf2008-07-26 23:55:29111
112 // Otherwise (or if that fails), create a new one.
[email protected]4566f132009-03-12 01:55:13113 if (!process_) {
[email protected]a6df5112009-01-21 23:50:15114 if (render_process_host_factory_) {
[email protected]4566f132009-03-12 01:55:13115 process_ = render_process_host_factory_->CreateRenderProcessHost(
[email protected]41fb79a52012-06-29 16:34:33116 browser_context);
[email protected]a6df5112009-01-21 23:50:15117 } else {
[email protected]4c3a23582012-08-18 08:54:34118 StoragePartitionImpl* partition =
119 static_cast<StoragePartitionImpl*>(
120 BrowserContext::GetStoragePartition(browser_context, this));
[email protected]7b38b192013-03-23 18:39:31121 bool supports_browser_plugin = GetContentClient()->browser()->
122 SupportsBrowserPlugin(browser_context, site_);
[email protected]3d7474ff2011-07-27 17:47:37123 process_ =
[email protected]4c3a23582012-08-18 08:54:34124 new RenderProcessHostImpl(browser_context, partition,
[email protected]7b38b192013-03-23 18:39:31125 supports_browser_plugin,
[email protected]25a9e8e532012-06-22 06:16:28126 site_.SchemeIs(chrome::kGuestScheme));
[email protected]a6df5112009-01-21 23:50:15127 }
128 }
[email protected]41fb79a52012-06-29 16:34:33129 CHECK(process_);
130
131 // If we are using process-per-site, we need to register this process
132 // for the current site so that we can find it again. (If no site is set
133 // at this time, we will register it in SetSite().)
134 if (use_process_per_site) {
135 RenderProcessHostImpl::RegisterProcessHostForSite(browser_context,
136 process_, site_);
137 }
initial.commit09911bf2008-07-26 23:55:29138
[email protected]46488322012-10-30 03:22:20139 GetContentClient()->browser()->SiteInstanceGotProcess(this);
[email protected]6f371442011-11-09 06:45:46140
[email protected]313b80bd2011-11-23 03:49:10141 if (has_site_)
142 LockToOrigin();
initial.commit09911bf2008-07-26 23:55:29143 }
[email protected]4566f132009-03-12 01:55:13144 DCHECK(process_);
initial.commit09911bf2008-07-26 23:55:29145
[email protected]4566f132009-03-12 01:55:13146 return process_;
initial.commit09911bf2008-07-26 23:55:29147}
148
[email protected]b6583592012-01-25 19:52:33149void SiteInstanceImpl::SetSite(const GURL& url) {
initial.commit09911bf2008-07-26 23:55:29150 // A SiteInstance's site should not change.
151 // TODO(creis): When following links or script navigations, we can currently
152 // render pages from other sites in this SiteInstance. This will eventually
153 // be fixed, but until then, we should still not set the site of a
154 // SiteInstance more than once.
155 DCHECK(!has_site_);
156
157 // Remember that this SiteInstance has been used to load a URL, even if the
158 // URL is invalid.
159 has_site_ = true;
[email protected]4c3a23582012-08-18 08:54:34160 BrowserContext* browser_context = browsing_instance_->browser_context();
[email protected]41fb79a52012-06-29 16:34:33161 site_ = GetSiteForURL(browser_context, url);
initial.commit09911bf2008-07-26 23:55:29162
163 // Now that we have a site, register it with the BrowsingInstance. This
164 // ensures that we won't create another SiteInstance for this site within
165 // the same BrowsingInstance, because all same-site pages within a
166 // BrowsingInstance can script each other.
167 browsing_instance_->RegisterSiteInstance(this);
[email protected]313b80bd2011-11-23 03:49:10168
[email protected]41fb79a52012-06-29 16:34:33169 if (process_) {
[email protected]313b80bd2011-11-23 03:49:10170 LockToOrigin();
[email protected]41fb79a52012-06-29 16:34:33171
172 // Ensure the process is registered for this site if necessary.
173 if (RenderProcessHostImpl::ShouldUseProcessPerSite(browser_context,
174 site_)) {
175 RenderProcessHostImpl::RegisterProcessHostForSite(
176 browser_context, process_, site_);
177 }
178 }
initial.commit09911bf2008-07-26 23:55:29179}
180
[email protected]77ab17312012-09-28 15:34:59181const GURL& SiteInstanceImpl::GetSiteURL() const {
[email protected]b6583592012-01-25 19:52:33182 return site_;
183}
184
185bool SiteInstanceImpl::HasSite() const {
186 return has_site_;
187}
188
189bool SiteInstanceImpl::HasRelatedSiteInstance(const GURL& url) {
initial.commit09911bf2008-07-26 23:55:29190 return browsing_instance_->HasSiteInstance(url);
191}
192
[email protected]b6583592012-01-25 19:52:33193SiteInstance* SiteInstanceImpl::GetRelatedSiteInstance(const GURL& url) {
initial.commit09911bf2008-07-26 23:55:29194 return browsing_instance_->GetSiteInstanceForURL(url);
195}
196
[email protected]14392a52012-05-02 20:28:44197bool SiteInstanceImpl::IsRelatedSiteInstance(const SiteInstance* instance) {
198 return browsing_instance_ ==
199 static_cast<const SiteInstanceImpl*>(instance)->browsing_instance_;
200}
201
[email protected]f88628d02012-11-11 17:58:59202bool SiteInstanceImpl::HasWrongProcessForURL(const GURL& url) {
[email protected]d292d8a2011-05-25 03:47:11203 // Having no process isn't a problem, since we'll assign it correctly.
[email protected]f88628d02012-11-11 17:58:59204 // Note that HasProcess() may return true if process_ is null, in
205 // process-per-site cases where there's an existing process available.
206 // We want to use such a process in the IsSuitableHost check, so we
207 // may end up assigning process_ in the GetProcess() call below.
208 if (!HasProcess())
[email protected]d292d8a2011-05-25 03:47:11209 return false;
210
[email protected]144a8102012-01-14 01:05:31211 // If the URL to navigate to can be associated with any site instance,
212 // we want to keep it in the same process.
213 if (IsURLSameAsAnySiteInstance(url))
214 return false;
215
[email protected]88aae972011-12-16 01:14:18216 // If the site URL is an extension (e.g., for hosted apps or WebUI) but the
[email protected]d292d8a2011-05-25 03:47:11217 // process is not (or vice versa), make sure we notice and fix it.
[email protected]2a5221b2011-09-27 23:07:31218 GURL site_url = GetSiteForURL(browsing_instance_->browser_context(), url);
[email protected]88aae972011-12-16 01:14:18219 return !RenderProcessHostImpl::IsSuitableHost(
[email protected]f88628d02012-11-11 17:58:59220 GetProcess(), browsing_instance_->browser_context(), site_url);
[email protected]d292d8a2011-05-25 03:47:11221}
222
[email protected]4c3a23582012-08-18 08:54:34223BrowserContext* SiteInstanceImpl::GetBrowserContext() const {
[email protected]72daaa92012-01-18 13:39:02224 return browsing_instance_->browser_context();
225}
226
initial.commit09911bf2008-07-26 23:55:29227/*static*/
[email protected]4c3a23582012-08-18 08:54:34228SiteInstance* SiteInstance::Create(BrowserContext* browser_context) {
[email protected]b6583592012-01-25 19:52:33229 return new SiteInstanceImpl(new BrowsingInstance(browser_context));
initial.commit09911bf2008-07-26 23:55:29230}
231
232/*static*/
[email protected]4c3a23582012-08-18 08:54:34233SiteInstance* SiteInstance::CreateForURL(BrowserContext* browser_context,
234 const GURL& url) {
[email protected]633fbc42009-05-07 23:39:47235 // This BrowsingInstance may be deleted if it returns an existing
236 // SiteInstance.
[email protected]3d7474ff2011-07-27 17:47:37237 scoped_refptr<BrowsingInstance> instance(
238 new BrowsingInstance(browser_context));
[email protected]633fbc42009-05-07 23:39:47239 return instance->GetSiteInstanceForURL(url);
[email protected]d8a96622009-05-07 19:21:16240}
241
242/*static*/
[email protected]399583b2012-12-11 09:33:42243bool SiteInstance::IsSameWebSite(BrowserContext* browser_context,
244 const GURL& real_url1,
245 const GURL& real_url2) {
246 GURL url1 = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url1);
247 GURL url2 = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url2);
248
249 // We infer web site boundaries based on the registered domain name of the
250 // top-level page and the scheme. We do not pay attention to the port if
251 // one is present, because pages served from different ports can still
252 // access each other if they change their document.domain variable.
253
254 // Some special URLs will match the site instance of any other URL. This is
255 // done before checking both of them for validity, since we want these URLs
256 // to have the same site instance as even an invalid one.
257 if (IsURLSameAsAnySiteInstance(url1) || IsURLSameAsAnySiteInstance(url2))
258 return true;
259
260 // If either URL is invalid, they aren't part of the same site.
261 if (!url1.is_valid() || !url2.is_valid())
262 return false;
263
264 // If the schemes differ, they aren't part of the same site.
265 if (url1.scheme() != url2.scheme())
266 return false;
267
[email protected]ed32c212013-05-14 20:49:29268 return net::registry_controlled_domains::SameDomainOrHost(
269 url1,
270 url2,
271 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
[email protected]399583b2012-12-11 09:33:42272}
273
274/*static*/
275GURL SiteInstance::GetSiteForURL(BrowserContext* browser_context,
276 const GURL& real_url) {
[email protected]25a9e8e532012-06-22 06:16:28277 // TODO(fsamuel, creis): For some reason appID is not recognized as a host.
278 if (real_url.SchemeIs(chrome::kGuestScheme))
279 return real_url;
280
[email protected]b6583592012-01-25 19:52:33281 GURL url = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url);
[email protected]3a8eecb2010-04-22 23:56:30282
initial.commit09911bf2008-07-26 23:55:29283 // URLs with no host should have an empty site.
284 GURL site;
285
286 // TODO(creis): For many protocols, we should just treat the scheme as the
[email protected]76a010b2008-12-07 23:48:03287 // site, since there is no host. e.g., file:, about:, chrome:
initial.commit09911bf2008-07-26 23:55:29288
289 // If the url has a host, then determine the site.
290 if (url.has_host()) {
[email protected]6705b232008-11-26 00:16:51291 // Only keep the scheme and registered domain as given by GetOrigin. This
292 // may also include a port, which we need to drop.
initial.commit09911bf2008-07-26 23:55:29293 site = url.GetOrigin();
294
[email protected]6705b232008-11-26 00:16:51295 // Remove port, if any.
296 if (site.has_port()) {
297 GURL::Replacements rep;
298 rep.ClearPort();
299 site = site.ReplaceComponents(rep);
300 }
301
initial.commit09911bf2008-07-26 23:55:29302 // If this URL has a registered domain, we only want to remember that part.
303 std::string domain =
[email protected]ed32c212013-05-14 20:49:29304 net::registry_controlled_domains::GetDomainAndRegistry(
305 url,
306 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
initial.commit09911bf2008-07-26 23:55:29307 if (!domain.empty()) {
308 GURL::Replacements rep;
309 rep.SetHostStr(domain);
310 site = site.ReplaceComponents(rep);
311 }
312 }
313 return site;
314}
315
316/*static*/
[email protected]4c3a23582012-08-18 08:54:34317GURL SiteInstanceImpl::GetEffectiveURL(BrowserContext* browser_context,
318 const GURL& url) {
[email protected]46488322012-10-30 03:22:20319 return GetContentClient()->browser()->
[email protected]3d7474ff2011-07-27 17:47:37320 GetEffectiveURL(browser_context, url);
[email protected]3a8eecb2010-04-22 23:56:30321}
322
[email protected]b6583592012-01-25 19:52:33323void SiteInstanceImpl::Observe(int type,
[email protected]46488322012-10-30 03:22:20324 const NotificationSource& source,
325 const NotificationDetails& details) {
326 DCHECK(type == NOTIFICATION_RENDERER_PROCESS_TERMINATED);
327 RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr();
[email protected]4566f132009-03-12 01:55:13328 if (rph == process_)
329 process_ = NULL;
330}
[email protected]313b80bd2011-11-23 03:49:10331
[email protected]b6583592012-01-25 19:52:33332void SiteInstanceImpl::LockToOrigin() {
[email protected]c6f2e672012-11-15 01:47:02333 // We currently only restrict this process to a particular site if the
334 // --enable-strict-site-isolation or --site-per-process flags are present.
[email protected]313b80bd2011-11-23 03:49:10335 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
[email protected]c6f2e672012-11-15 01:47:02336 if (command_line.HasSwitch(switches::kEnableStrictSiteIsolation) ||
337 command_line.HasSwitch(switches::kSitePerProcess)) {
[email protected]b9535422012-02-09 01:47:59338 ChildProcessSecurityPolicyImpl* policy =
339 ChildProcessSecurityPolicyImpl::GetInstance();
[email protected]313b80bd2011-11-23 03:49:10340 policy->LockToOrigin(process_->GetID(), site_);
341 }
342}
[email protected]46488322012-10-30 03:22:20343
344} // namespace content