[email protected] | 80a8fad | 2011-01-29 04:02:38 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 5 | #include "content/browser/site_instance.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
[email protected] | 7e3abea4 | 2011-02-18 00:51:10 | [diff] [blame] | 7 | #include "chrome/common/url_constants.h" |
[email protected] | 3936521 | 2011-02-24 01:01:00 | [diff] [blame] | 8 | #include "content/browser/browsing_instance.h" |
[email protected] | 1fd1a50 | 2011-03-30 16:55:56 | [diff] [blame] | 9 | #include "content/browser/content_browser_client.h" |
[email protected] | 05fcf98 | 2011-04-19 00:44:14 | [diff] [blame^] | 10 | #include "content/browser/renderer_host/browser_render_process_host.h" |
[email protected] | 67fc039 | 2011-02-25 02:56:57 | [diff] [blame] | 11 | #include "content/browser/webui/web_ui_factory.h" |
[email protected] | 7f070d4 | 2011-03-09 20:25:32 | [diff] [blame] | 12 | #include "content/common/notification_service.h" |
[email protected] | 1fd1a50 | 2011-03-30 16:55:56 | [diff] [blame] | 13 | #include "content/common/content_client.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | #include "net/base/registry_controlled_domain.h" |
| 15 | |
[email protected] | 2ee0418e | 2009-06-26 21:00:43 | [diff] [blame] | 16 | // We treat javascript:, about:crash, about:hang, and about:shorthang as the |
| 17 | // same site as any URL since they are actually modifiers on existing pages. |
| 18 | static bool IsURLSameAsAnySiteInstance(const GURL& url) { |
| 19 | if (!url.is_valid()) |
| 20 | return false; |
| 21 | return url.SchemeIs(chrome::kJavaScriptScheme) || |
| 22 | url.spec() == chrome::kAboutCrashURL || |
[email protected] | a8f02439 | 2011-01-13 21:50:16 | [diff] [blame] | 23 | url.spec() == chrome::kAboutKillURL || |
[email protected] | 2ee0418e | 2009-06-26 21:00:43 | [diff] [blame] | 24 | url.spec() == chrome::kAboutHangURL || |
| 25 | url.spec() == chrome::kAboutShorthangURL; |
| 26 | } |
| 27 | |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 28 | SiteInstance::SiteInstance(BrowsingInstance* browsing_instance) |
| 29 | : browsing_instance_(browsing_instance), |
| 30 | render_process_host_factory_(NULL), |
| 31 | process_(NULL), |
| 32 | max_page_id_(-1), |
| 33 | has_site_(false) { |
| 34 | DCHECK(browsing_instance); |
| 35 | |
[email protected] | 5be94df | 2009-05-22 18:41:32 | [diff] [blame] | 36 | registrar_.Add(this, NotificationType::RENDERER_PROCESS_TERMINATED, |
| 37 | NotificationService::AllSources()); |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 38 | } |
| 39 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 40 | SiteInstance::~SiteInstance() { |
| 41 | // 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_) |
| 45 | browsing_instance_->UnregisterSiteInstance(this); |
| 46 | } |
| 47 | |
[email protected] | 5ab79b0 | 2010-04-26 16:47:11 | [diff] [blame] | 48 | bool SiteInstance::HasProcess() const { |
| 49 | return (process_ != NULL); |
| 50 | } |
| 51 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 52 | RenderProcessHost* SiteInstance::GetProcess() { |
[email protected] | 08c8ec7e | 2010-07-11 17:14:48 | [diff] [blame] | 53 | // TODO(erikkay) It would be nice to ensure that the renderer type had been |
| 54 | // properly set before we get here. The default tab creation case winds up |
| 55 | // with no site set at this point, so it will default to TYPE_NORMAL. This |
| 56 | // may not be correct, so we'll wind up potentially creating a process that |
| 57 | // we then throw away, or worse sharing a process with the wrong process type. |
| 58 | // See crbug.com/43448. |
| 59 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 60 | // Create a new process if ours went away or was reused. |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 61 | if (!process_) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 62 | // See if we should reuse an old process |
| 63 | if (RenderProcessHost::ShouldTryToUseExistingProcessHost()) |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 64 | process_ = RenderProcessHost::GetExistingProcessHost( |
[email protected] | 1dfa950 | 2009-06-15 20:28:09 | [diff] [blame] | 65 | browsing_instance_->profile(), GetRendererType()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 66 | |
| 67 | // Otherwise (or if that fails), create a new one. |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 68 | if (!process_) { |
[email protected] | a6df511 | 2009-01-21 23:50:15 | [diff] [blame] | 69 | if (render_process_host_factory_) { |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 70 | process_ = render_process_host_factory_->CreateRenderProcessHost( |
[email protected] | a6df511 | 2009-01-21 23:50:15 | [diff] [blame] | 71 | browsing_instance_->profile()); |
| 72 | } else { |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 73 | process_ = new BrowserRenderProcessHost(browsing_instance_->profile()); |
[email protected] | a6df511 | 2009-01-21 23:50:15 | [diff] [blame] | 74 | } |
| 75 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 76 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 77 | // Make sure the process starts at the right max_page_id |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 78 | process_->UpdateMaxPageID(max_page_id_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 79 | } |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 80 | DCHECK(process_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 81 | |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 82 | return process_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void SiteInstance::SetSite(const GURL& url) { |
| 86 | // A SiteInstance's site should not change. |
| 87 | // TODO(creis): When following links or script navigations, we can currently |
| 88 | // render pages from other sites in this SiteInstance. This will eventually |
| 89 | // be fixed, but until then, we should still not set the site of a |
| 90 | // SiteInstance more than once. |
| 91 | DCHECK(!has_site_); |
| 92 | |
| 93 | // Remember that this SiteInstance has been used to load a URL, even if the |
| 94 | // URL is invalid. |
| 95 | has_site_ = true; |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 96 | site_ = GetSiteForURL(browsing_instance_->profile(), url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 97 | |
| 98 | // Now that we have a site, register it with the BrowsingInstance. This |
| 99 | // ensures that we won't create another SiteInstance for this site within |
| 100 | // the same BrowsingInstance, because all same-site pages within a |
| 101 | // BrowsingInstance can script each other. |
| 102 | browsing_instance_->RegisterSiteInstance(this); |
| 103 | } |
| 104 | |
| 105 | bool SiteInstance::HasRelatedSiteInstance(const GURL& url) { |
| 106 | return browsing_instance_->HasSiteInstance(url); |
| 107 | } |
| 108 | |
| 109 | SiteInstance* SiteInstance::GetRelatedSiteInstance(const GURL& url) { |
| 110 | return browsing_instance_->GetSiteInstanceForURL(url); |
| 111 | } |
| 112 | |
| 113 | /*static*/ |
| 114 | SiteInstance* SiteInstance::CreateSiteInstance(Profile* profile) { |
| 115 | return new SiteInstance(new BrowsingInstance(profile)); |
| 116 | } |
| 117 | |
| 118 | /*static*/ |
[email protected] | d8a9662 | 2009-05-07 19:21:16 | [diff] [blame] | 119 | SiteInstance* SiteInstance::CreateSiteInstanceForURL(Profile* profile, |
| 120 | const GURL& url) { |
[email protected] | 633fbc4 | 2009-05-07 23:39:47 | [diff] [blame] | 121 | // This BrowsingInstance may be deleted if it returns an existing |
| 122 | // SiteInstance. |
| 123 | scoped_refptr<BrowsingInstance> instance(new BrowsingInstance(profile)); |
| 124 | return instance->GetSiteInstanceForURL(url); |
[email protected] | d8a9662 | 2009-05-07 19:21:16 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /*static*/ |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 128 | GURL SiteInstance::GetSiteForURL(Profile* profile, const GURL& real_url) { |
| 129 | GURL url = GetEffectiveURL(profile, real_url); |
| 130 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 131 | // URLs with no host should have an empty site. |
| 132 | GURL site; |
| 133 | |
| 134 | // TODO(creis): For many protocols, we should just treat the scheme as the |
[email protected] | 76a010b | 2008-12-07 23:48:03 | [diff] [blame] | 135 | // site, since there is no host. e.g., file:, about:, chrome: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 136 | |
| 137 | // If the url has a host, then determine the site. |
| 138 | if (url.has_host()) { |
[email protected] | 6705b23 | 2008-11-26 00:16:51 | [diff] [blame] | 139 | // Only keep the scheme and registered domain as given by GetOrigin. This |
| 140 | // may also include a port, which we need to drop. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 141 | site = url.GetOrigin(); |
| 142 | |
[email protected] | 6705b23 | 2008-11-26 00:16:51 | [diff] [blame] | 143 | // Remove port, if any. |
| 144 | if (site.has_port()) { |
| 145 | GURL::Replacements rep; |
| 146 | rep.ClearPort(); |
| 147 | site = site.ReplaceComponents(rep); |
| 148 | } |
| 149 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 150 | // If this URL has a registered domain, we only want to remember that part. |
| 151 | std::string domain = |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 152 | net::RegistryControlledDomainService::GetDomainAndRegistry(url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 153 | if (!domain.empty()) { |
| 154 | GURL::Replacements rep; |
| 155 | rep.SetHostStr(domain); |
| 156 | site = site.ReplaceComponents(rep); |
| 157 | } |
| 158 | } |
| 159 | return site; |
| 160 | } |
| 161 | |
| 162 | /*static*/ |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 163 | bool SiteInstance::IsSameWebSite(Profile* profile, |
| 164 | const GURL& real_url1, const GURL& real_url2) { |
| 165 | GURL url1 = GetEffectiveURL(profile, real_url1); |
| 166 | GURL url2 = GetEffectiveURL(profile, real_url2); |
| 167 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 168 | // We infer web site boundaries based on the registered domain name of the |
[email protected] | 6705b23 | 2008-11-26 00:16:51 | [diff] [blame] | 169 | // top-level page and the scheme. We do not pay attention to the port if |
| 170 | // one is present, because pages served from different ports can still |
| 171 | // access each other if they change their document.domain variable. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 172 | |
[email protected] | 2ee0418e | 2009-06-26 21:00:43 | [diff] [blame] | 173 | // Some special URLs will match the site instance of any other URL. This is |
| 174 | // done before checking both of them for validity, since we want these URLs |
| 175 | // to have the same site instance as even an invalid one. |
| 176 | if (IsURLSameAsAnySiteInstance(url1) || IsURLSameAsAnySiteInstance(url2)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 177 | return true; |
| 178 | |
| 179 | // If either URL is invalid, they aren't part of the same site. |
[email protected] | 2ee0418e | 2009-06-26 21:00:43 | [diff] [blame] | 180 | if (!url1.is_valid() || !url2.is_valid()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 181 | return false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 182 | |
[email protected] | 6705b23 | 2008-11-26 00:16:51 | [diff] [blame] | 183 | // If the schemes differ, they aren't part of the same site. |
[email protected] | 2ee0418e | 2009-06-26 21:00:43 | [diff] [blame] | 184 | if (url1.scheme() != url2.scheme()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 185 | return false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 186 | |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 187 | return net::RegistryControlledDomainService::SameDomainOrHost(url1, url2); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 188 | } |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 189 | |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 190 | /*static*/ |
| 191 | GURL SiteInstance::GetEffectiveURL(Profile* profile, const GURL& url) { |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 192 | return content::GetContentClient()->browser()->GetEffectiveURL(profile, url); |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 193 | } |
| 194 | |
[email protected] | 08c8ec7e | 2010-07-11 17:14:48 | [diff] [blame] | 195 | /*static*/ |
| 196 | RenderProcessHost::Type SiteInstance::RendererTypeForURL(const GURL& url) { |
| 197 | if (!url.is_valid()) |
[email protected] | 1dfa950 | 2009-06-15 20:28:09 | [diff] [blame] | 198 | return RenderProcessHost::TYPE_NORMAL; |
| 199 | |
[email protected] | 08c8ec7e | 2010-07-11 17:14:48 | [diff] [blame] | 200 | if (url.SchemeIs(chrome::kExtensionScheme)) |
[email protected] | 1dfa950 | 2009-06-15 20:28:09 | [diff] [blame] | 201 | return RenderProcessHost::TYPE_EXTENSION; |
| 202 | |
[email protected] | 80a8fad | 2011-01-29 04:02:38 | [diff] [blame] | 203 | // TODO(erikkay) creis recommends using UseWebUIForURL instead. |
[email protected] | 1fd1a50 | 2011-03-30 16:55:56 | [diff] [blame] | 204 | if (content::WebUIFactory::Get()->HasWebUIScheme(url)) |
[email protected] | d098079 | 2011-02-13 19:41:40 | [diff] [blame] | 205 | return RenderProcessHost::TYPE_WEBUI; |
[email protected] | 1dfa950 | 2009-06-15 20:28:09 | [diff] [blame] | 206 | |
| 207 | return RenderProcessHost::TYPE_NORMAL; |
| 208 | } |
| 209 | |
[email protected] | 08c8ec7e | 2010-07-11 17:14:48 | [diff] [blame] | 210 | RenderProcessHost::Type SiteInstance::GetRendererType() { |
| 211 | // We may not have a site at this point, which generally means this is a |
| 212 | // normal navigation. |
| 213 | if (!has_site_) |
| 214 | return RenderProcessHost::TYPE_NORMAL; |
| 215 | |
| 216 | return RendererTypeForURL(site_); |
| 217 | } |
| 218 | |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 219 | void SiteInstance::Observe(NotificationType type, |
| 220 | const NotificationSource& source, |
| 221 | const NotificationDetails& details) { |
| 222 | DCHECK(type == NotificationType::RENDERER_PROCESS_TERMINATED); |
| 223 | RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr(); |
| 224 | if (rph == process_) |
| 225 | process_ = NULL; |
| 226 | } |