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