blob: feb5402cca182004f6cddde97a834bba911d4924 [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]313b80bd2011-11-23 03:49:109#include "content/browser/child_process_security_policy.h"
[email protected]f3b1a082011-11-18 00:34:3010#include "content/browser/renderer_host/render_process_host_impl.h"
[email protected]87f3c082011-10-19 18:07:4411#include "content/public/browser/content_browser_client.h"
[email protected]ad50def52011-10-19 23:17:0712#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1613#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:3014#include "content/public/browser/render_process_host_factory.h"
[email protected]313b80bd2011-11-23 03:49:1015#include "content/public/common/content_switches.h"
[email protected]a1d29162011-10-14 17:14:0316#include "content/public/common/url_constants.h"
initial.commit09911bf2008-07-26 23:55:2917#include "net/base/registry_controlled_domain.h"
18
[email protected]b6583592012-01-25 19:52:3319using content::SiteInstance;
20
[email protected]2ee0418e2009-06-26 21:00:4321static bool IsURLSameAsAnySiteInstance(const GURL& url) {
22 if (!url.is_valid())
23 return false;
[email protected]0f012df82011-05-19 14:15:2924
[email protected]89f550b2011-06-08 18:34:0325 // We treat javascript: as the same site as any URL since it is actually
26 // a modifier on existing pages.
27 if (url.SchemeIs(chrome::kJavaScriptScheme))
[email protected]0f012df82011-05-19 14:15:2928 return true;
[email protected]0f012df82011-05-19 14:15:2929
30 return
31 content::GetContentClient()->browser()->IsURLSameAsAnySiteInstance(url);
[email protected]2ee0418e2009-06-26 21:00:4332}
33
[email protected]b6583592012-01-25 19:52:3334int32 SiteInstanceImpl::next_site_instance_id_ = 1;
[email protected]992db4c2011-05-12 15:37:1535
[email protected]b6583592012-01-25 19:52:3336SiteInstanceImpl::SiteInstanceImpl(BrowsingInstance* browsing_instance)
[email protected]992db4c2011-05-12 15:37:1537 : id_(next_site_instance_id_++),
38 browsing_instance_(browsing_instance),
[email protected]4566f132009-03-12 01:55:1339 render_process_host_factory_(NULL),
40 process_(NULL),
[email protected]4566f132009-03-12 01:55:1341 has_site_(false) {
42 DCHECK(browsing_instance);
43
[email protected]432115822011-07-10 15:52:2744 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:0745 content::NotificationService::AllBrowserContextsAndSources());
[email protected]4566f132009-03-12 01:55:1346}
47
[email protected]b6583592012-01-25 19:52:3348SiteInstanceImpl::~SiteInstanceImpl() {
[email protected]6f371442011-11-09 06:45:4649 content::GetContentClient()->browser()->SiteInstanceDeleting(this);
[email protected]056ad2a2011-07-12 02:13:5550
initial.commit09911bf2008-07-26 23:55:2951 // Now that no one is referencing us, we can safely remove ourselves from
52 // the BrowsingInstance. Any future visits to a page from this site
53 // (within the same BrowsingInstance) can safely create a new SiteInstance.
54 if (has_site_)
[email protected]b6583592012-01-25 19:52:3355 browsing_instance_->UnregisterSiteInstance(
56 static_cast<SiteInstance*>(this));
initial.commit09911bf2008-07-26 23:55:2957}
58
[email protected]b6583592012-01-25 19:52:3359int32 SiteInstanceImpl::GetId() {
60 return id_;
61}
62
63bool SiteInstanceImpl::HasProcess() const {
[email protected]5ab79b02010-04-26 16:47:1164 return (process_ != NULL);
65}
66
[email protected]b6583592012-01-25 19:52:3367content::RenderProcessHost* SiteInstanceImpl::GetProcess() {
[email protected]08c8ec7e2010-07-11 17:14:4868 // TODO(erikkay) It would be nice to ensure that the renderer type had been
69 // properly set before we get here. The default tab creation case winds up
70 // with no site set at this point, so it will default to TYPE_NORMAL. This
71 // may not be correct, so we'll wind up potentially creating a process that
72 // we then throw away, or worse sharing a process with the wrong process type.
73 // See crbug.com/43448.
74
initial.commit09911bf2008-07-26 23:55:2975 // Create a new process if ours went away or was reused.
[email protected]4566f132009-03-12 01:55:1376 if (!process_) {
initial.commit09911bf2008-07-26 23:55:2977 // See if we should reuse an old process
[email protected]f3b1a082011-11-18 00:34:3078 if (content::RenderProcessHost::ShouldTryToUseExistingProcessHost())
79 process_ = content::RenderProcessHost::GetExistingProcessHost(
[email protected]2a5221b2011-09-27 23:07:3180 browsing_instance_->browser_context(), site_);
initial.commit09911bf2008-07-26 23:55:2981
82 // Otherwise (or if that fails), create a new one.
[email protected]4566f132009-03-12 01:55:1383 if (!process_) {
[email protected]a6df5112009-01-21 23:50:1584 if (render_process_host_factory_) {
[email protected]4566f132009-03-12 01:55:1385 process_ = render_process_host_factory_->CreateRenderProcessHost(
[email protected]3d7474ff2011-07-27 17:47:3786 browsing_instance_->browser_context());
[email protected]a6df5112009-01-21 23:50:1587 } else {
[email protected]3d7474ff2011-07-27 17:47:3788 process_ =
[email protected]f3b1a082011-11-18 00:34:3089 new RenderProcessHostImpl(browsing_instance_->browser_context());
[email protected]a6df5112009-01-21 23:50:1590 }
91 }
initial.commit09911bf2008-07-26 23:55:2992
[email protected]6f371442011-11-09 06:45:4693 content::GetContentClient()->browser()->SiteInstanceGotProcess(this);
94
[email protected]313b80bd2011-11-23 03:49:1095 if (has_site_)
96 LockToOrigin();
initial.commit09911bf2008-07-26 23:55:2997 }
[email protected]4566f132009-03-12 01:55:1398 DCHECK(process_);
initial.commit09911bf2008-07-26 23:55:2999
[email protected]4566f132009-03-12 01:55:13100 return process_;
initial.commit09911bf2008-07-26 23:55:29101}
102
[email protected]b6583592012-01-25 19:52:33103void SiteInstanceImpl::SetSite(const GURL& url) {
initial.commit09911bf2008-07-26 23:55:29104 // A SiteInstance's site should not change.
105 // TODO(creis): When following links or script navigations, we can currently
106 // render pages from other sites in this SiteInstance. This will eventually
107 // be fixed, but until then, we should still not set the site of a
108 // SiteInstance more than once.
109 DCHECK(!has_site_);
110
111 // Remember that this SiteInstance has been used to load a URL, even if the
112 // URL is invalid.
113 has_site_ = true;
[email protected]3d7474ff2011-07-27 17:47:37114 site_ = GetSiteForURL(browsing_instance_->browser_context(), url);
initial.commit09911bf2008-07-26 23:55:29115
116 // Now that we have a site, register it with the BrowsingInstance. This
117 // ensures that we won't create another SiteInstance for this site within
118 // the same BrowsingInstance, because all same-site pages within a
119 // BrowsingInstance can script each other.
120 browsing_instance_->RegisterSiteInstance(this);
[email protected]313b80bd2011-11-23 03:49:10121
122 if (process_)
123 LockToOrigin();
initial.commit09911bf2008-07-26 23:55:29124}
125
[email protected]b6583592012-01-25 19:52:33126const GURL& SiteInstanceImpl::GetSite() const {
127 return site_;
128}
129
130bool SiteInstanceImpl::HasSite() const {
131 return has_site_;
132}
133
134bool SiteInstanceImpl::HasRelatedSiteInstance(const GURL& url) {
initial.commit09911bf2008-07-26 23:55:29135 return browsing_instance_->HasSiteInstance(url);
136}
137
[email protected]b6583592012-01-25 19:52:33138SiteInstance* SiteInstanceImpl::GetRelatedSiteInstance(const GURL& url) {
initial.commit09911bf2008-07-26 23:55:29139 return browsing_instance_->GetSiteInstanceForURL(url);
140}
141
[email protected]b6583592012-01-25 19:52:33142bool SiteInstanceImpl::HasWrongProcessForURL(const GURL& url) const {
[email protected]d292d8a2011-05-25 03:47:11143 // Having no process isn't a problem, since we'll assign it correctly.
144 if (!HasProcess())
145 return false;
146
[email protected]144a8102012-01-14 01:05:31147 // If the URL to navigate to can be associated with any site instance,
148 // we want to keep it in the same process.
149 if (IsURLSameAsAnySiteInstance(url))
150 return false;
151
[email protected]88aae972011-12-16 01:14:18152 // If the site URL is an extension (e.g., for hosted apps or WebUI) but the
[email protected]d292d8a2011-05-25 03:47:11153 // process is not (or vice versa), make sure we notice and fix it.
[email protected]2a5221b2011-09-27 23:07:31154 GURL site_url = GetSiteForURL(browsing_instance_->browser_context(), url);
[email protected]88aae972011-12-16 01:14:18155 return !RenderProcessHostImpl::IsSuitableHost(
156 process_, browsing_instance_->browser_context(), site_url);
[email protected]d292d8a2011-05-25 03:47:11157}
158
[email protected]b6583592012-01-25 19:52:33159content::BrowserContext* SiteInstanceImpl::GetBrowserContext() const {
[email protected]72daaa92012-01-18 13:39:02160 return browsing_instance_->browser_context();
161}
162
initial.commit09911bf2008-07-26 23:55:29163/*static*/
[email protected]b6583592012-01-25 19:52:33164SiteInstance* SiteInstance::Create(content::BrowserContext* browser_context) {
165 return new SiteInstanceImpl(new BrowsingInstance(browser_context));
initial.commit09911bf2008-07-26 23:55:29166}
167
168/*static*/
[email protected]b6583592012-01-25 19:52:33169SiteInstance* SiteInstance::CreateForURL(
[email protected]3d7474ff2011-07-27 17:47:37170 content::BrowserContext* browser_context, const GURL& url) {
[email protected]633fbc42009-05-07 23:39:47171 // This BrowsingInstance may be deleted if it returns an existing
172 // SiteInstance.
[email protected]3d7474ff2011-07-27 17:47:37173 scoped_refptr<BrowsingInstance> instance(
174 new BrowsingInstance(browser_context));
[email protected]633fbc42009-05-07 23:39:47175 return instance->GetSiteInstanceForURL(url);
[email protected]d8a96622009-05-07 19:21:16176}
177
178/*static*/
[email protected]b6583592012-01-25 19:52:33179GURL SiteInstanceImpl::GetSiteForURL(content::BrowserContext* browser_context,
180 const GURL& real_url) {
181 GURL url = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url);
[email protected]3a8eecb2010-04-22 23:56:30182
initial.commit09911bf2008-07-26 23:55:29183 // URLs with no host should have an empty site.
184 GURL site;
185
186 // TODO(creis): For many protocols, we should just treat the scheme as the
[email protected]76a010b2008-12-07 23:48:03187 // site, since there is no host. e.g., file:, about:, chrome:
initial.commit09911bf2008-07-26 23:55:29188
189 // If the url has a host, then determine the site.
190 if (url.has_host()) {
[email protected]6705b232008-11-26 00:16:51191 // Only keep the scheme and registered domain as given by GetOrigin. This
192 // may also include a port, which we need to drop.
initial.commit09911bf2008-07-26 23:55:29193 site = url.GetOrigin();
194
[email protected]6705b232008-11-26 00:16:51195 // Remove port, if any.
196 if (site.has_port()) {
197 GURL::Replacements rep;
198 rep.ClearPort();
199 site = site.ReplaceComponents(rep);
200 }
201
initial.commit09911bf2008-07-26 23:55:29202 // If this URL has a registered domain, we only want to remember that part.
203 std::string domain =
[email protected]8ac1a752008-07-31 19:40:37204 net::RegistryControlledDomainService::GetDomainAndRegistry(url);
initial.commit09911bf2008-07-26 23:55:29205 if (!domain.empty()) {
206 GURL::Replacements rep;
207 rep.SetHostStr(domain);
208 site = site.ReplaceComponents(rep);
209 }
210 }
211 return site;
212}
213
214/*static*/
[email protected]3d7474ff2011-07-27 17:47:37215bool SiteInstance::IsSameWebSite(content::BrowserContext* browser_context,
[email protected]b6583592012-01-25 19:52:33216 const GURL& real_url1,
217 const GURL& real_url2) {
218 GURL url1 = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url1);
219 GURL url2 = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url2);
[email protected]3a8eecb2010-04-22 23:56:30220
initial.commit09911bf2008-07-26 23:55:29221 // We infer web site boundaries based on the registered domain name of the
[email protected]6705b232008-11-26 00:16:51222 // top-level page and the scheme. We do not pay attention to the port if
223 // one is present, because pages served from different ports can still
224 // access each other if they change their document.domain variable.
initial.commit09911bf2008-07-26 23:55:29225
[email protected]2ee0418e2009-06-26 21:00:43226 // Some special URLs will match the site instance of any other URL. This is
227 // done before checking both of them for validity, since we want these URLs
228 // to have the same site instance as even an invalid one.
229 if (IsURLSameAsAnySiteInstance(url1) || IsURLSameAsAnySiteInstance(url2))
initial.commit09911bf2008-07-26 23:55:29230 return true;
231
232 // If either URL is invalid, they aren't part of the same site.
[email protected]2ee0418e2009-06-26 21:00:43233 if (!url1.is_valid() || !url2.is_valid())
initial.commit09911bf2008-07-26 23:55:29234 return false;
initial.commit09911bf2008-07-26 23:55:29235
[email protected]6705b232008-11-26 00:16:51236 // If the schemes differ, they aren't part of the same site.
[email protected]2ee0418e2009-06-26 21:00:43237 if (url1.scheme() != url2.scheme())
initial.commit09911bf2008-07-26 23:55:29238 return false;
initial.commit09911bf2008-07-26 23:55:29239
[email protected]8ac1a752008-07-31 19:40:37240 return net::RegistryControlledDomainService::SameDomainOrHost(url1, url2);
initial.commit09911bf2008-07-26 23:55:29241}
[email protected]4566f132009-03-12 01:55:13242
[email protected]3a8eecb2010-04-22 23:56:30243/*static*/
[email protected]b6583592012-01-25 19:52:33244GURL SiteInstanceImpl::GetEffectiveURL(
245 content::BrowserContext* browser_context,
246 const GURL& url) {
[email protected]3d7474ff2011-07-27 17:47:37247 return content::GetContentClient()->browser()->
248 GetEffectiveURL(browser_context, url);
[email protected]3a8eecb2010-04-22 23:56:30249}
250
[email protected]b6583592012-01-25 19:52:33251void SiteInstanceImpl::Observe(int type,
252 const content::NotificationSource& source,
253 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:27254 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED);
[email protected]f3b1a082011-11-18 00:34:30255 content::RenderProcessHost* rph =
256 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]4566f132009-03-12 01:55:13257 if (rph == process_)
258 process_ = NULL;
259}
[email protected]313b80bd2011-11-23 03:49:10260
[email protected]b6583592012-01-25 19:52:33261void SiteInstanceImpl::LockToOrigin() {
[email protected]313b80bd2011-11-23 03:49:10262 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
263 if (command_line.HasSwitch(switches::kEnableStrictSiteIsolation)) {
264 ChildProcessSecurityPolicy* policy =
265 ChildProcessSecurityPolicy::GetInstance();
266 policy->LockToOrigin(process_->GetID(), site_);
267 }
268}
269