blob: 9fecc165f719e8a21ba79de81c63b88a446d7b3f [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]1d8a3d1f2011-02-19 07:11:525#include "content/browser/site_instance.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]2ee0418e2009-06-26 21:00:4319static bool IsURLSameAsAnySiteInstance(const GURL& url) {
20 if (!url.is_valid())
21 return false;
[email protected]0f012df82011-05-19 14:15:2922
[email protected]89f550b2011-06-08 18:34:0323 // We treat javascript: as the same site as any URL since it is actually
24 // a modifier on existing pages.
25 if (url.SchemeIs(chrome::kJavaScriptScheme))
[email protected]0f012df82011-05-19 14:15:2926 return true;
[email protected]0f012df82011-05-19 14:15:2927
28 return
29 content::GetContentClient()->browser()->IsURLSameAsAnySiteInstance(url);
[email protected]2ee0418e2009-06-26 21:00:4330}
31
[email protected]992db4c2011-05-12 15:37:1532int32 SiteInstance::next_site_instance_id_ = 1;
33
[email protected]4566f132009-03-12 01:55:1334SiteInstance::SiteInstance(BrowsingInstance* browsing_instance)
[email protected]992db4c2011-05-12 15:37:1535 : id_(next_site_instance_id_++),
36 browsing_instance_(browsing_instance),
[email protected]4566f132009-03-12 01:55:1337 render_process_host_factory_(NULL),
38 process_(NULL),
[email protected]4566f132009-03-12 01:55:1339 has_site_(false) {
40 DCHECK(browsing_instance);
41
[email protected]432115822011-07-10 15:52:2742 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:0743 content::NotificationService::AllBrowserContextsAndSources());
[email protected]4566f132009-03-12 01:55:1344}
45
initial.commit09911bf2008-07-26 23:55:2946SiteInstance::~SiteInstance() {
[email protected]6f371442011-11-09 06:45:4647 content::GetContentClient()->browser()->SiteInstanceDeleting(this);
[email protected]056ad2a2011-07-12 02:13:5548
initial.commit09911bf2008-07-26 23:55:2949 // Now that no one is referencing us, we can safely remove ourselves from
50 // the BrowsingInstance. Any future visits to a page from this site
51 // (within the same BrowsingInstance) can safely create a new SiteInstance.
52 if (has_site_)
53 browsing_instance_->UnregisterSiteInstance(this);
54}
55
[email protected]5ab79b02010-04-26 16:47:1156bool SiteInstance::HasProcess() const {
57 return (process_ != NULL);
58}
59
[email protected]f3b1a082011-11-18 00:34:3060content::RenderProcessHost* SiteInstance::GetProcess() {
[email protected]08c8ec7e2010-07-11 17:14:4861 // TODO(erikkay) It would be nice to ensure that the renderer type had been
62 // properly set before we get here. The default tab creation case winds up
63 // with no site set at this point, so it will default to TYPE_NORMAL. This
64 // may not be correct, so we'll wind up potentially creating a process that
65 // we then throw away, or worse sharing a process with the wrong process type.
66 // See crbug.com/43448.
67
initial.commit09911bf2008-07-26 23:55:2968 // Create a new process if ours went away or was reused.
[email protected]4566f132009-03-12 01:55:1369 if (!process_) {
initial.commit09911bf2008-07-26 23:55:2970 // See if we should reuse an old process
[email protected]f3b1a082011-11-18 00:34:3071 if (content::RenderProcessHost::ShouldTryToUseExistingProcessHost())
72 process_ = content::RenderProcessHost::GetExistingProcessHost(
[email protected]2a5221b2011-09-27 23:07:3173 browsing_instance_->browser_context(), site_);
initial.commit09911bf2008-07-26 23:55:2974
75 // Otherwise (or if that fails), create a new one.
[email protected]4566f132009-03-12 01:55:1376 if (!process_) {
[email protected]a6df5112009-01-21 23:50:1577 if (render_process_host_factory_) {
[email protected]4566f132009-03-12 01:55:1378 process_ = render_process_host_factory_->CreateRenderProcessHost(
[email protected]3d7474ff2011-07-27 17:47:3779 browsing_instance_->browser_context());
[email protected]a6df5112009-01-21 23:50:1580 } else {
[email protected]3d7474ff2011-07-27 17:47:3781 process_ =
[email protected]f3b1a082011-11-18 00:34:3082 new RenderProcessHostImpl(browsing_instance_->browser_context());
[email protected]a6df5112009-01-21 23:50:1583 }
84 }
initial.commit09911bf2008-07-26 23:55:2985
[email protected]6f371442011-11-09 06:45:4686 content::GetContentClient()->browser()->SiteInstanceGotProcess(this);
87
[email protected]313b80bd2011-11-23 03:49:1088 if (has_site_)
89 LockToOrigin();
initial.commit09911bf2008-07-26 23:55:2990 }
[email protected]4566f132009-03-12 01:55:1391 DCHECK(process_);
initial.commit09911bf2008-07-26 23:55:2992
[email protected]4566f132009-03-12 01:55:1393 return process_;
initial.commit09911bf2008-07-26 23:55:2994}
95
96void SiteInstance::SetSite(const GURL& url) {
97 // A SiteInstance's site should not change.
98 // TODO(creis): When following links or script navigations, we can currently
99 // render pages from other sites in this SiteInstance. This will eventually
100 // be fixed, but until then, we should still not set the site of a
101 // SiteInstance more than once.
102 DCHECK(!has_site_);
103
104 // Remember that this SiteInstance has been used to load a URL, even if the
105 // URL is invalid.
106 has_site_ = true;
[email protected]3d7474ff2011-07-27 17:47:37107 site_ = GetSiteForURL(browsing_instance_->browser_context(), url);
initial.commit09911bf2008-07-26 23:55:29108
109 // Now that we have a site, register it with the BrowsingInstance. This
110 // ensures that we won't create another SiteInstance for this site within
111 // the same BrowsingInstance, because all same-site pages within a
112 // BrowsingInstance can script each other.
113 browsing_instance_->RegisterSiteInstance(this);
[email protected]313b80bd2011-11-23 03:49:10114
115 if (process_)
116 LockToOrigin();
initial.commit09911bf2008-07-26 23:55:29117}
118
119bool SiteInstance::HasRelatedSiteInstance(const GURL& url) {
120 return browsing_instance_->HasSiteInstance(url);
121}
122
123SiteInstance* SiteInstance::GetRelatedSiteInstance(const GURL& url) {
124 return browsing_instance_->GetSiteInstanceForURL(url);
125}
126
[email protected]d292d8a2011-05-25 03:47:11127bool SiteInstance::HasWrongProcessForURL(const GURL& url) const {
128 // Having no process isn't a problem, since we'll assign it correctly.
129 if (!HasProcess())
130 return false;
131
[email protected]144a8102012-01-14 01:05:31132 // If the URL to navigate to can be associated with any site instance,
133 // we want to keep it in the same process.
134 if (IsURLSameAsAnySiteInstance(url))
135 return false;
136
[email protected]88aae972011-12-16 01:14:18137 // If the site URL is an extension (e.g., for hosted apps or WebUI) but the
[email protected]d292d8a2011-05-25 03:47:11138 // process is not (or vice versa), make sure we notice and fix it.
[email protected]2a5221b2011-09-27 23:07:31139 GURL site_url = GetSiteForURL(browsing_instance_->browser_context(), url);
[email protected]88aae972011-12-16 01:14:18140 return !RenderProcessHostImpl::IsSuitableHost(
141 process_, browsing_instance_->browser_context(), site_url);
[email protected]d292d8a2011-05-25 03:47:11142}
143
[email protected]72daaa92012-01-18 13:39:02144content::BrowserContext* SiteInstance::GetBrowserContext() const {
145 return browsing_instance_->browser_context();
146}
147
initial.commit09911bf2008-07-26 23:55:29148/*static*/
[email protected]3d7474ff2011-07-27 17:47:37149SiteInstance* SiteInstance::CreateSiteInstance(
150 content::BrowserContext* browser_context) {
151 return new SiteInstance(new BrowsingInstance(browser_context));
initial.commit09911bf2008-07-26 23:55:29152}
153
154/*static*/
[email protected]3d7474ff2011-07-27 17:47:37155SiteInstance* SiteInstance::CreateSiteInstanceForURL(
156 content::BrowserContext* browser_context, const GURL& url) {
[email protected]633fbc42009-05-07 23:39:47157 // This BrowsingInstance may be deleted if it returns an existing
158 // SiteInstance.
[email protected]3d7474ff2011-07-27 17:47:37159 scoped_refptr<BrowsingInstance> instance(
160 new BrowsingInstance(browser_context));
[email protected]633fbc42009-05-07 23:39:47161 return instance->GetSiteInstanceForURL(url);
[email protected]d8a96622009-05-07 19:21:16162}
163
164/*static*/
[email protected]3d7474ff2011-07-27 17:47:37165GURL SiteInstance::GetSiteForURL(content::BrowserContext* browser_context,
166 const GURL& real_url) {
167 GURL url = GetEffectiveURL(browser_context, real_url);
[email protected]3a8eecb2010-04-22 23:56:30168
initial.commit09911bf2008-07-26 23:55:29169 // URLs with no host should have an empty site.
170 GURL site;
171
172 // TODO(creis): For many protocols, we should just treat the scheme as the
[email protected]76a010b2008-12-07 23:48:03173 // site, since there is no host. e.g., file:, about:, chrome:
initial.commit09911bf2008-07-26 23:55:29174
175 // If the url has a host, then determine the site.
176 if (url.has_host()) {
[email protected]6705b232008-11-26 00:16:51177 // Only keep the scheme and registered domain as given by GetOrigin. This
178 // may also include a port, which we need to drop.
initial.commit09911bf2008-07-26 23:55:29179 site = url.GetOrigin();
180
[email protected]6705b232008-11-26 00:16:51181 // Remove port, if any.
182 if (site.has_port()) {
183 GURL::Replacements rep;
184 rep.ClearPort();
185 site = site.ReplaceComponents(rep);
186 }
187
initial.commit09911bf2008-07-26 23:55:29188 // If this URL has a registered domain, we only want to remember that part.
189 std::string domain =
[email protected]8ac1a752008-07-31 19:40:37190 net::RegistryControlledDomainService::GetDomainAndRegistry(url);
initial.commit09911bf2008-07-26 23:55:29191 if (!domain.empty()) {
192 GURL::Replacements rep;
193 rep.SetHostStr(domain);
194 site = site.ReplaceComponents(rep);
195 }
196 }
197 return site;
198}
199
200/*static*/
[email protected]3d7474ff2011-07-27 17:47:37201bool SiteInstance::IsSameWebSite(content::BrowserContext* browser_context,
[email protected]3a8eecb2010-04-22 23:56:30202 const GURL& real_url1, const GURL& real_url2) {
[email protected]3d7474ff2011-07-27 17:47:37203 GURL url1 = GetEffectiveURL(browser_context, real_url1);
204 GURL url2 = GetEffectiveURL(browser_context, real_url2);
[email protected]3a8eecb2010-04-22 23:56:30205
initial.commit09911bf2008-07-26 23:55:29206 // We infer web site boundaries based on the registered domain name of the
[email protected]6705b232008-11-26 00:16:51207 // top-level page and the scheme. We do not pay attention to the port if
208 // one is present, because pages served from different ports can still
209 // access each other if they change their document.domain variable.
initial.commit09911bf2008-07-26 23:55:29210
[email protected]2ee0418e2009-06-26 21:00:43211 // Some special URLs will match the site instance of any other URL. This is
212 // done before checking both of them for validity, since we want these URLs
213 // to have the same site instance as even an invalid one.
214 if (IsURLSameAsAnySiteInstance(url1) || IsURLSameAsAnySiteInstance(url2))
initial.commit09911bf2008-07-26 23:55:29215 return true;
216
217 // If either URL is invalid, they aren't part of the same site.
[email protected]2ee0418e2009-06-26 21:00:43218 if (!url1.is_valid() || !url2.is_valid())
initial.commit09911bf2008-07-26 23:55:29219 return false;
initial.commit09911bf2008-07-26 23:55:29220
[email protected]6705b232008-11-26 00:16:51221 // If the schemes differ, they aren't part of the same site.
[email protected]2ee0418e2009-06-26 21:00:43222 if (url1.scheme() != url2.scheme())
initial.commit09911bf2008-07-26 23:55:29223 return false;
initial.commit09911bf2008-07-26 23:55:29224
[email protected]8ac1a752008-07-31 19:40:37225 return net::RegistryControlledDomainService::SameDomainOrHost(url1, url2);
initial.commit09911bf2008-07-26 23:55:29226}
[email protected]4566f132009-03-12 01:55:13227
[email protected]3a8eecb2010-04-22 23:56:30228/*static*/
[email protected]3d7474ff2011-07-27 17:47:37229GURL SiteInstance::GetEffectiveURL(content::BrowserContext* browser_context,
230 const GURL& url) {
231 return content::GetContentClient()->browser()->
232 GetEffectiveURL(browser_context, url);
[email protected]3a8eecb2010-04-22 23:56:30233}
234
[email protected]432115822011-07-10 15:52:27235void SiteInstance::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:53236 const content::NotificationSource& source,
237 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:27238 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED);
[email protected]f3b1a082011-11-18 00:34:30239 content::RenderProcessHost* rph =
240 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]4566f132009-03-12 01:55:13241 if (rph == process_)
242 process_ = NULL;
243}
[email protected]313b80bd2011-11-23 03:49:10244
245void SiteInstance::LockToOrigin() {
246 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
247 if (command_line.HasSwitch(switches::kEnableStrictSiteIsolation)) {
248 ChildProcessSecurityPolicy* policy =
249 ChildProcessSecurityPolicy::GetInstance();
250 policy->LockToOrigin(process_->GetID(), site_);
251 }
252}
253