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