blob: a59d1ca0ea270bf885b14715bffc6170c1918579 [file] [log] [blame]
[email protected]80a8fad2011-01-29 04:02:381// Copyright (c) 2011 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]eaa7dd182010-12-14 11:09:007#include "chrome/browser/extensions/extension_service.h"
[email protected]1d8a3d1f2011-02-19 07:11:528#include "chrome/browser/renderer_host/browser_render_process_host.h"
[email protected]7e3abea42011-02-18 00:51:109#include "chrome/common/url_constants.h"
[email protected]39365212011-02-24 01:01:0010#include "content/browser/browsing_instance.h"
[email protected]67fc0392011-02-25 02:56:5711#include "content/browser/webui/web_ui_factory.h"
[email protected]7f070d42011-03-09 20:25:3212#include "content/common/notification_service.h"
initial.commit09911bf2008-07-26 23:55:2913#include "net/base/registry_controlled_domain.h"
14
[email protected]2ee0418e2009-06-26 21:00:4315// We treat javascript:, about:crash, about:hang, and about:shorthang as the
16// same site as any URL since they are actually modifiers on existing pages.
17static bool IsURLSameAsAnySiteInstance(const GURL& url) {
18 if (!url.is_valid())
19 return false;
20 return url.SchemeIs(chrome::kJavaScriptScheme) ||
21 url.spec() == chrome::kAboutCrashURL ||
[email protected]a8f024392011-01-13 21:50:1622 url.spec() == chrome::kAboutKillURL ||
[email protected]2ee0418e2009-06-26 21:00:4323 url.spec() == chrome::kAboutHangURL ||
24 url.spec() == chrome::kAboutShorthangURL;
25}
26
[email protected]4566f132009-03-12 01:55:1327SiteInstance::SiteInstance(BrowsingInstance* browsing_instance)
28 : browsing_instance_(browsing_instance),
29 render_process_host_factory_(NULL),
30 process_(NULL),
31 max_page_id_(-1),
32 has_site_(false) {
33 DCHECK(browsing_instance);
34
[email protected]5be94df2009-05-22 18:41:3235 registrar_.Add(this, NotificationType::RENDERER_PROCESS_TERMINATED,
36 NotificationService::AllSources());
[email protected]4566f132009-03-12 01:55:1337}
38
initial.commit09911bf2008-07-26 23:55:2939SiteInstance::~SiteInstance() {
40 // Now that no one is referencing us, we can safely remove ourselves from
41 // the BrowsingInstance. Any future visits to a page from this site
42 // (within the same BrowsingInstance) can safely create a new SiteInstance.
43 if (has_site_)
44 browsing_instance_->UnregisterSiteInstance(this);
45}
46
[email protected]5ab79b02010-04-26 16:47:1147bool SiteInstance::HasProcess() const {
48 return (process_ != NULL);
49}
50
initial.commit09911bf2008-07-26 23:55:2951RenderProcessHost* SiteInstance::GetProcess() {
[email protected]08c8ec7e2010-07-11 17:14:4852 // TODO(erikkay) It would be nice to ensure that the renderer type had been
53 // properly set before we get here. The default tab creation case winds up
54 // with no site set at this point, so it will default to TYPE_NORMAL. This
55 // may not be correct, so we'll wind up potentially creating a process that
56 // we then throw away, or worse sharing a process with the wrong process type.
57 // See crbug.com/43448.
58
initial.commit09911bf2008-07-26 23:55:2959 // Create a new process if ours went away or was reused.
[email protected]4566f132009-03-12 01:55:1360 if (!process_) {
initial.commit09911bf2008-07-26 23:55:2961 // See if we should reuse an old process
62 if (RenderProcessHost::ShouldTryToUseExistingProcessHost())
[email protected]4566f132009-03-12 01:55:1363 process_ = RenderProcessHost::GetExistingProcessHost(
[email protected]1dfa9502009-06-15 20:28:0964 browsing_instance_->profile(), GetRendererType());
initial.commit09911bf2008-07-26 23:55:2965
66 // Otherwise (or if that fails), create a new one.
[email protected]4566f132009-03-12 01:55:1367 if (!process_) {
[email protected]a6df5112009-01-21 23:50:1568 if (render_process_host_factory_) {
[email protected]4566f132009-03-12 01:55:1369 process_ = render_process_host_factory_->CreateRenderProcessHost(
[email protected]a6df5112009-01-21 23:50:1570 browsing_instance_->profile());
71 } else {
[email protected]4566f132009-03-12 01:55:1372 process_ = new BrowserRenderProcessHost(browsing_instance_->profile());
[email protected]a6df5112009-01-21 23:50:1573 }
74 }
initial.commit09911bf2008-07-26 23:55:2975
initial.commit09911bf2008-07-26 23:55:2976 // Make sure the process starts at the right max_page_id
[email protected]4566f132009-03-12 01:55:1377 process_->UpdateMaxPageID(max_page_id_);
initial.commit09911bf2008-07-26 23:55:2978 }
[email protected]4566f132009-03-12 01:55:1379 DCHECK(process_);
initial.commit09911bf2008-07-26 23:55:2980
[email protected]4566f132009-03-12 01:55:1381 return process_;
initial.commit09911bf2008-07-26 23:55:2982}
83
84void SiteInstance::SetSite(const GURL& url) {
85 // A SiteInstance's site should not change.
86 // TODO(creis): When following links or script navigations, we can currently
87 // render pages from other sites in this SiteInstance. This will eventually
88 // be fixed, but until then, we should still not set the site of a
89 // SiteInstance more than once.
90 DCHECK(!has_site_);
91
92 // Remember that this SiteInstance has been used to load a URL, even if the
93 // URL is invalid.
94 has_site_ = true;
[email protected]3a8eecb2010-04-22 23:56:3095 site_ = GetSiteForURL(browsing_instance_->profile(), url);
initial.commit09911bf2008-07-26 23:55:2996
97 // Now that we have a site, register it with the BrowsingInstance. This
98 // ensures that we won't create another SiteInstance for this site within
99 // the same BrowsingInstance, because all same-site pages within a
100 // BrowsingInstance can script each other.
101 browsing_instance_->RegisterSiteInstance(this);
102}
103
104bool SiteInstance::HasRelatedSiteInstance(const GURL& url) {
105 return browsing_instance_->HasSiteInstance(url);
106}
107
108SiteInstance* SiteInstance::GetRelatedSiteInstance(const GURL& url) {
109 return browsing_instance_->GetSiteInstanceForURL(url);
110}
111
112/*static*/
113SiteInstance* SiteInstance::CreateSiteInstance(Profile* profile) {
114 return new SiteInstance(new BrowsingInstance(profile));
115}
116
117/*static*/
[email protected]d8a96622009-05-07 19:21:16118SiteInstance* SiteInstance::CreateSiteInstanceForURL(Profile* profile,
119 const GURL& url) {
[email protected]633fbc42009-05-07 23:39:47120 // This BrowsingInstance may be deleted if it returns an existing
121 // SiteInstance.
122 scoped_refptr<BrowsingInstance> instance(new BrowsingInstance(profile));
123 return instance->GetSiteInstanceForURL(url);
[email protected]d8a96622009-05-07 19:21:16124}
125
126/*static*/
[email protected]3a8eecb2010-04-22 23:56:30127GURL SiteInstance::GetSiteForURL(Profile* profile, const GURL& real_url) {
128 GURL url = GetEffectiveURL(profile, real_url);
129
initial.commit09911bf2008-07-26 23:55:29130 // URLs with no host should have an empty site.
131 GURL site;
132
133 // TODO(creis): For many protocols, we should just treat the scheme as the
[email protected]76a010b2008-12-07 23:48:03134 // site, since there is no host. e.g., file:, about:, chrome:
initial.commit09911bf2008-07-26 23:55:29135
136 // If the url has a host, then determine the site.
137 if (url.has_host()) {
[email protected]6705b232008-11-26 00:16:51138 // Only keep the scheme and registered domain as given by GetOrigin. This
139 // may also include a port, which we need to drop.
initial.commit09911bf2008-07-26 23:55:29140 site = url.GetOrigin();
141
[email protected]6705b232008-11-26 00:16:51142 // Remove port, if any.
143 if (site.has_port()) {
144 GURL::Replacements rep;
145 rep.ClearPort();
146 site = site.ReplaceComponents(rep);
147 }
148
initial.commit09911bf2008-07-26 23:55:29149 // If this URL has a registered domain, we only want to remember that part.
150 std::string domain =
[email protected]8ac1a752008-07-31 19:40:37151 net::RegistryControlledDomainService::GetDomainAndRegistry(url);
initial.commit09911bf2008-07-26 23:55:29152 if (!domain.empty()) {
153 GURL::Replacements rep;
154 rep.SetHostStr(domain);
155 site = site.ReplaceComponents(rep);
156 }
157 }
158 return site;
159}
160
161/*static*/
[email protected]3a8eecb2010-04-22 23:56:30162bool SiteInstance::IsSameWebSite(Profile* profile,
163 const GURL& real_url1, const GURL& real_url2) {
164 GURL url1 = GetEffectiveURL(profile, real_url1);
165 GURL url2 = GetEffectiveURL(profile, real_url2);
166
initial.commit09911bf2008-07-26 23:55:29167 // We infer web site boundaries based on the registered domain name of the
[email protected]6705b232008-11-26 00:16:51168 // top-level page and the scheme. We do not pay attention to the port if
169 // one is present, because pages served from different ports can still
170 // access each other if they change their document.domain variable.
initial.commit09911bf2008-07-26 23:55:29171
[email protected]2ee0418e2009-06-26 21:00:43172 // Some special URLs will match the site instance of any other URL. This is
173 // done before checking both of them for validity, since we want these URLs
174 // to have the same site instance as even an invalid one.
175 if (IsURLSameAsAnySiteInstance(url1) || IsURLSameAsAnySiteInstance(url2))
initial.commit09911bf2008-07-26 23:55:29176 return true;
177
178 // If either URL is invalid, they aren't part of the same site.
[email protected]2ee0418e2009-06-26 21:00:43179 if (!url1.is_valid() || !url2.is_valid())
initial.commit09911bf2008-07-26 23:55:29180 return false;
initial.commit09911bf2008-07-26 23:55:29181
[email protected]6705b232008-11-26 00:16:51182 // If the schemes differ, they aren't part of the same site.
[email protected]2ee0418e2009-06-26 21:00:43183 if (url1.scheme() != url2.scheme())
initial.commit09911bf2008-07-26 23:55:29184 return false;
initial.commit09911bf2008-07-26 23:55:29185
[email protected]8ac1a752008-07-31 19:40:37186 return net::RegistryControlledDomainService::SameDomainOrHost(url1, url2);
initial.commit09911bf2008-07-26 23:55:29187}
[email protected]4566f132009-03-12 01:55:13188
[email protected]3a8eecb2010-04-22 23:56:30189/*static*/
190GURL SiteInstance::GetEffectiveURL(Profile* profile, const GURL& url) {
[email protected]eaa7dd182010-12-14 11:09:00191 if (!profile || !profile->GetExtensionService())
[email protected]3a8eecb2010-04-22 23:56:30192 return url;
193
[email protected]9adb9692010-10-29 23:14:02194 const Extension* extension =
[email protected]eaa7dd182010-12-14 11:09:00195 profile->GetExtensionService()->GetExtensionByWebExtent(url);
[email protected]3a8eecb2010-04-22 23:56:30196 if (extension) {
197 // If the URL is part of an extension's web extent, convert it to an
198 // extension URL.
199 return extension->GetResourceURL(url.path());
200 } else {
201 return url;
202 }
203}
204
[email protected]08c8ec7e2010-07-11 17:14:48205/*static*/
206RenderProcessHost::Type SiteInstance::RendererTypeForURL(const GURL& url) {
207 if (!url.is_valid())
[email protected]1dfa9502009-06-15 20:28:09208 return RenderProcessHost::TYPE_NORMAL;
209
[email protected]08c8ec7e2010-07-11 17:14:48210 if (url.SchemeIs(chrome::kExtensionScheme))
[email protected]1dfa9502009-06-15 20:28:09211 return RenderProcessHost::TYPE_EXTENSION;
212
[email protected]80a8fad2011-01-29 04:02:38213 // TODO(erikkay) creis recommends using UseWebUIForURL instead.
214 if (WebUIFactory::HasWebUIScheme(url))
[email protected]d0980792011-02-13 19:41:40215 return RenderProcessHost::TYPE_WEBUI;
[email protected]1dfa9502009-06-15 20:28:09216
217 return RenderProcessHost::TYPE_NORMAL;
218}
219
[email protected]08c8ec7e2010-07-11 17:14:48220RenderProcessHost::Type SiteInstance::GetRendererType() {
221 // We may not have a site at this point, which generally means this is a
222 // normal navigation.
223 if (!has_site_)
224 return RenderProcessHost::TYPE_NORMAL;
225
226 return RendererTypeForURL(site_);
227}
228
[email protected]4566f132009-03-12 01:55:13229void SiteInstance::Observe(NotificationType type,
230 const NotificationSource& source,
231 const NotificationDetails& details) {
232 DCHECK(type == NotificationType::RENDERER_PROCESS_TERMINATED);
233 RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr();
234 if (rph == process_)
235 process_ = NULL;
236}