| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/site_instance.h" | 5 #include "content/browser/site_instance_impl.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "content/browser/browsing_instance.h" | 8 #include "content/browser/browsing_instance.h" |
| 9 #include "content/browser/child_process_security_policy.h" | 9 #include "content/browser/child_process_security_policy.h" |
| 10 #include "content/browser/renderer_host/render_process_host_impl.h" | 10 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 11 #include "content/public/browser/content_browser_client.h" | 11 #include "content/public/browser/content_browser_client.h" |
| 12 #include "content/public/browser/notification_service.h" | 12 #include "content/public/browser/notification_service.h" |
| 13 #include "content/public/browser/notification_types.h" | 13 #include "content/public/browser/notification_types.h" |
| 14 #include "content/public/browser/render_process_host_factory.h" | 14 #include "content/public/browser/render_process_host_factory.h" |
| 15 #include "content/public/common/content_switches.h" | 15 #include "content/public/common/content_switches.h" |
| 16 #include "content/public/common/url_constants.h" | 16 #include "content/public/common/url_constants.h" |
| 17 #include "net/base/registry_controlled_domain.h" | 17 #include "net/base/registry_controlled_domain.h" |
| 18 | 18 |
| 19 static bool IsURLSameAsAnySiteInstance(const GURL& url) { | 19 static bool IsURLSameAsAnySiteInstance(const GURL& url) { |
| 20 if (!url.is_valid()) | 20 if (!url.is_valid()) |
| 21 return false; | 21 return false; |
| 22 | 22 |
| 23 // We treat javascript: as the same site as any URL since it is actually | 23 // We treat javascript: as the same site as any URL since it is actually |
| 24 // a modifier on existing pages. | 24 // a modifier on existing pages. |
| 25 if (url.SchemeIs(chrome::kJavaScriptScheme)) | 25 if (url.SchemeIs(chrome::kJavaScriptScheme)) |
| 26 return true; | 26 return true; |
| 27 | 27 |
| 28 return | 28 return |
| 29 content::GetContentClient()->browser()->IsURLSameAsAnySiteInstance(url); | 29 content::GetContentClient()->browser()->IsURLSameAsAnySiteInstance(url); |
| 30 } | 30 } |
| 31 | 31 |
| 32 int32 SiteInstance::next_site_instance_id_ = 1; | 32 int32 SiteInstanceImpl::next_site_instance_id_ = 1; |
| 33 | 33 |
| 34 SiteInstance::SiteInstance(BrowsingInstance* browsing_instance) | 34 SiteInstanceImpl::SiteInstanceImpl(BrowsingInstance* browsing_instance) |
| 35 : id_(next_site_instance_id_++), | 35 : id_(next_site_instance_id_++), |
| 36 browsing_instance_(browsing_instance), | 36 browsing_instance_(browsing_instance), |
| 37 render_process_host_factory_(NULL), | 37 render_process_host_factory_(NULL), |
| 38 process_(NULL), | 38 process_(NULL), |
| 39 has_site_(false) { | 39 has_site_(false) { |
| 40 DCHECK(browsing_instance); | 40 DCHECK(browsing_instance); |
| 41 | 41 |
| 42 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 42 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 43 content::NotificationService::AllBrowserContextsAndSources()); | 43 content::NotificationService::AllBrowserContextsAndSources()); |
| 44 } | 44 } |
| 45 | 45 |
| 46 SiteInstance::~SiteInstance() { | 46 SiteInstanceImpl::~SiteInstanceImpl() { |
| 47 content::GetContentClient()->browser()->SiteInstanceDeleting(this); | 47 content::GetContentClient()->browser()->SiteInstanceDeleting(this); |
| 48 | 48 |
| 49 // Now that no one is referencing us, we can safely remove ourselves from | 49 // 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 | 50 // the BrowsingInstance. Any future visits to a page from this site |
| 51 // (within the same BrowsingInstance) can safely create a new SiteInstance. | 51 // (within the same BrowsingInstance) can safely create a new SiteInstance. |
| 52 if (has_site_) | 52 if (has_site_) |
| 53 browsing_instance_->UnregisterSiteInstance(this); | 53 browsing_instance_->UnregisterSiteInstance( |
| 54 static_cast<content::SiteInstance*>(this)); |
| 54 } | 55 } |
| 55 | 56 |
| 56 bool SiteInstance::HasProcess() const { | 57 int32 SiteInstanceImpl::GetId() { |
| 58 return id_; |
| 59 } |
| 60 |
| 61 bool SiteInstanceImpl::HasProcess() const { |
| 57 return (process_ != NULL); | 62 return (process_ != NULL); |
| 58 } | 63 } |
| 59 | 64 |
| 60 content::RenderProcessHost* SiteInstance::GetProcess() { | 65 content::RenderProcessHost* SiteInstanceImpl::GetProcess() { |
| 61 // TODO(erikkay) It would be nice to ensure that the renderer type had been | 66 // 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 | 67 // 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 | 68 // 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 | 69 // 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. | 70 // we then throw away, or worse sharing a process with the wrong process type. |
| 66 // See crbug.com/43448. | 71 // See crbug.com/43448. |
| 67 | 72 |
| 68 // Create a new process if ours went away or was reused. | 73 // Create a new process if ours went away or was reused. |
| 69 if (!process_) { | 74 if (!process_) { |
| 70 // See if we should reuse an old process | 75 // See if we should reuse an old process |
| (...skipping 15 matching lines...) Expand all Loading... |
| 86 content::GetContentClient()->browser()->SiteInstanceGotProcess(this); | 91 content::GetContentClient()->browser()->SiteInstanceGotProcess(this); |
| 87 | 92 |
| 88 if (has_site_) | 93 if (has_site_) |
| 89 LockToOrigin(); | 94 LockToOrigin(); |
| 90 } | 95 } |
| 91 DCHECK(process_); | 96 DCHECK(process_); |
| 92 | 97 |
| 93 return process_; | 98 return process_; |
| 94 } | 99 } |
| 95 | 100 |
| 96 void SiteInstance::SetSite(const GURL& url) { | 101 void SiteInstanceImpl::SetSite(const GURL& url) { |
| 97 // A SiteInstance's site should not change. | 102 // A SiteInstance's site should not change. |
| 98 // TODO(creis): When following links or script navigations, we can currently | 103 // TODO(creis): When following links or script navigations, we can currently |
| 99 // render pages from other sites in this SiteInstance. This will eventually | 104 // 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 | 105 // be fixed, but until then, we should still not set the site of a |
| 101 // SiteInstance more than once. | 106 // SiteInstance more than once. |
| 102 DCHECK(!has_site_); | 107 DCHECK(!has_site_); |
| 103 | 108 |
| 104 // Remember that this SiteInstance has been used to load a URL, even if the | 109 // Remember that this SiteInstance has been used to load a URL, even if the |
| 105 // URL is invalid. | 110 // URL is invalid. |
| 106 has_site_ = true; | 111 has_site_ = true; |
| 107 site_ = GetSiteForURL(browsing_instance_->browser_context(), url); | 112 site_ = GetSiteForURL(browsing_instance_->browser_context(), url); |
| 108 | 113 |
| 109 // Now that we have a site, register it with the BrowsingInstance. This | 114 // 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 | 115 // ensures that we won't create another SiteInstance for this site within |
| 111 // the same BrowsingInstance, because all same-site pages within a | 116 // the same BrowsingInstance, because all same-site pages within a |
| 112 // BrowsingInstance can script each other. | 117 // BrowsingInstance can script each other. |
| 113 browsing_instance_->RegisterSiteInstance(this); | 118 browsing_instance_->RegisterSiteInstance(this); |
| 114 | 119 |
| 115 if (process_) | 120 if (process_) |
| 116 LockToOrigin(); | 121 LockToOrigin(); |
| 117 } | 122 } |
| 118 | 123 |
| 119 bool SiteInstance::HasRelatedSiteInstance(const GURL& url) { | 124 const GURL& SiteInstanceImpl::GetSite() const { |
| 125 return site_; |
| 126 } |
| 127 |
| 128 bool SiteInstanceImpl::HasSite() const { |
| 129 return has_site_; |
| 130 } |
| 131 |
| 132 bool SiteInstanceImpl::HasRelatedSiteInstance(const GURL& url) { |
| 120 return browsing_instance_->HasSiteInstance(url); | 133 return browsing_instance_->HasSiteInstance(url); |
| 121 } | 134 } |
| 122 | 135 |
| 123 SiteInstance* SiteInstance::GetRelatedSiteInstance(const GURL& url) { | 136 content::SiteInstance* SiteInstanceImpl::GetRelatedSiteInstance( |
| 137 const GURL& url) { |
| 124 return browsing_instance_->GetSiteInstanceForURL(url); | 138 return browsing_instance_->GetSiteInstanceForURL(url); |
| 125 } | 139 } |
| 126 | 140 |
| 127 bool SiteInstance::HasWrongProcessForURL(const GURL& url) const { | 141 bool SiteInstanceImpl::HasWrongProcessForURL(const GURL& url) const { |
| 128 // Having no process isn't a problem, since we'll assign it correctly. | 142 // Having no process isn't a problem, since we'll assign it correctly. |
| 129 if (!HasProcess()) | 143 if (!HasProcess()) |
| 130 return false; | 144 return false; |
| 131 | 145 |
| 132 // If the URL to navigate to can be associated with any site instance, | 146 // If the URL to navigate to can be associated with any site instance, |
| 133 // we want to keep it in the same process. | 147 // we want to keep it in the same process. |
| 134 if (IsURLSameAsAnySiteInstance(url)) | 148 if (IsURLSameAsAnySiteInstance(url)) |
| 135 return false; | 149 return false; |
| 136 | 150 |
| 137 // If the site URL is an extension (e.g., for hosted apps or WebUI) but the | 151 // If the site URL is an extension (e.g., for hosted apps or WebUI) but the |
| 138 // process is not (or vice versa), make sure we notice and fix it. | 152 // process is not (or vice versa), make sure we notice and fix it. |
| 139 GURL site_url = GetSiteForURL(browsing_instance_->browser_context(), url); | 153 GURL site_url = GetSiteForURL(browsing_instance_->browser_context(), url); |
| 140 return !RenderProcessHostImpl::IsSuitableHost( | 154 return !RenderProcessHostImpl::IsSuitableHost( |
| 141 process_, browsing_instance_->browser_context(), site_url); | 155 process_, browsing_instance_->browser_context(), site_url); |
| 142 } | 156 } |
| 143 | 157 |
| 144 content::BrowserContext* SiteInstance::GetBrowserContext() const { | 158 content::BrowserContext* SiteInstanceImpl::GetBrowserContext() const { |
| 145 return browsing_instance_->browser_context(); | 159 return browsing_instance_->browser_context(); |
| 146 } | 160 } |
| 147 | 161 |
| 148 /*static*/ | 162 BrowsingInstance* SiteInstanceImpl::GetBrowsingInstance() const { |
| 149 SiteInstance* SiteInstance::CreateSiteInstance( | 163 return browsing_instance_; |
| 150 content::BrowserContext* browser_context) { | |
| 151 return new SiteInstance(new BrowsingInstance(browser_context)); | |
| 152 } | 164 } |
| 153 | 165 |
| 154 /*static*/ | 166 /*static*/ |
| 155 SiteInstance* SiteInstance::CreateSiteInstanceForURL( | 167 content::SiteInstance* content::SiteInstance::CreateSiteInstance( |
| 168 content::BrowserContext* browser_context) { |
| 169 return new SiteInstanceImpl(new BrowsingInstance(browser_context)); |
| 170 } |
| 171 |
| 172 /*static*/ |
| 173 content::SiteInstance* content::SiteInstance::CreateSiteInstanceForURL( |
| 156 content::BrowserContext* browser_context, const GURL& url) { | 174 content::BrowserContext* browser_context, const GURL& url) { |
| 157 // This BrowsingInstance may be deleted if it returns an existing | 175 // This BrowsingInstance may be deleted if it returns an existing |
| 158 // SiteInstance. | 176 // SiteInstance. |
| 159 scoped_refptr<BrowsingInstance> instance( | 177 scoped_refptr<BrowsingInstance> instance( |
| 160 new BrowsingInstance(browser_context)); | 178 new BrowsingInstance(browser_context)); |
| 161 return instance->GetSiteInstanceForURL(url); | 179 return instance->GetSiteInstanceForURL(url); |
| 162 } | 180 } |
| 163 | 181 |
| 164 /*static*/ | 182 /*static*/ |
| 165 GURL SiteInstance::GetSiteForURL(content::BrowserContext* browser_context, | 183 GURL content::SiteInstance::GetSiteForURL( |
| 166 const GURL& real_url) { | 184 content::BrowserContext* browser_context, const GURL& real_url) { |
| 167 GURL url = GetEffectiveURL(browser_context, real_url); | 185 GURL url = GetEffectiveURL(browser_context, real_url); |
| 168 | 186 |
| 169 // URLs with no host should have an empty site. | 187 // URLs with no host should have an empty site. |
| 170 GURL site; | 188 GURL site; |
| 171 | 189 |
| 172 // TODO(creis): For many protocols, we should just treat the scheme as the | 190 // TODO(creis): For many protocols, we should just treat the scheme as the |
| 173 // site, since there is no host. e.g., file:, about:, chrome: | 191 // site, since there is no host. e.g., file:, about:, chrome: |
| 174 | 192 |
| 175 // If the url has a host, then determine the site. | 193 // If the url has a host, then determine the site. |
| 176 if (url.has_host()) { | 194 if (url.has_host()) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 191 if (!domain.empty()) { | 209 if (!domain.empty()) { |
| 192 GURL::Replacements rep; | 210 GURL::Replacements rep; |
| 193 rep.SetHostStr(domain); | 211 rep.SetHostStr(domain); |
| 194 site = site.ReplaceComponents(rep); | 212 site = site.ReplaceComponents(rep); |
| 195 } | 213 } |
| 196 } | 214 } |
| 197 return site; | 215 return site; |
| 198 } | 216 } |
| 199 | 217 |
| 200 /*static*/ | 218 /*static*/ |
| 201 bool SiteInstance::IsSameWebSite(content::BrowserContext* browser_context, | 219 bool content::SiteInstance::IsSameWebSite( |
| 202 const GURL& real_url1, const GURL& real_url2) { | 220 content::BrowserContext* browser_context, const GURL& real_url1, |
| 221 const GURL& real_url2) { |
| 203 GURL url1 = GetEffectiveURL(browser_context, real_url1); | 222 GURL url1 = GetEffectiveURL(browser_context, real_url1); |
| 204 GURL url2 = GetEffectiveURL(browser_context, real_url2); | 223 GURL url2 = GetEffectiveURL(browser_context, real_url2); |
| 205 | 224 |
| 206 // We infer web site boundaries based on the registered domain name of the | 225 // We infer web site boundaries based on the registered domain name of the |
| 207 // top-level page and the scheme. We do not pay attention to the port if | 226 // 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 | 227 // one is present, because pages served from different ports can still |
| 209 // access each other if they change their document.domain variable. | 228 // access each other if they change their document.domain variable. |
| 210 | 229 |
| 211 // Some special URLs will match the site instance of any other URL. This is | 230 // 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 | 231 // 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. | 232 // to have the same site instance as even an invalid one. |
| 214 if (IsURLSameAsAnySiteInstance(url1) || IsURLSameAsAnySiteInstance(url2)) | 233 if (IsURLSameAsAnySiteInstance(url1) || IsURLSameAsAnySiteInstance(url2)) |
| 215 return true; | 234 return true; |
| 216 | 235 |
| 217 // If either URL is invalid, they aren't part of the same site. | 236 // If either URL is invalid, they aren't part of the same site. |
| 218 if (!url1.is_valid() || !url2.is_valid()) | 237 if (!url1.is_valid() || !url2.is_valid()) |
| 219 return false; | 238 return false; |
| 220 | 239 |
| 221 // If the schemes differ, they aren't part of the same site. | 240 // If the schemes differ, they aren't part of the same site. |
| 222 if (url1.scheme() != url2.scheme()) | 241 if (url1.scheme() != url2.scheme()) |
| 223 return false; | 242 return false; |
| 224 | 243 |
| 225 return net::RegistryControlledDomainService::SameDomainOrHost(url1, url2); | 244 return net::RegistryControlledDomainService::SameDomainOrHost(url1, url2); |
| 226 } | 245 } |
| 227 | 246 |
| 228 /*static*/ | 247 /*static*/ |
| 229 GURL SiteInstance::GetEffectiveURL(content::BrowserContext* browser_context, | 248 GURL content::SiteInstance::GetEffectiveURL( |
| 230 const GURL& url) { | 249 content::BrowserContext* browser_context, const GURL& url) { |
| 231 return content::GetContentClient()->browser()-> | 250 return content::GetContentClient()->browser()-> |
| 232 GetEffectiveURL(browser_context, url); | 251 GetEffectiveURL(browser_context, url); |
| 233 } | 252 } |
| 234 | 253 |
| 235 void SiteInstance::Observe(int type, | 254 void SiteInstanceImpl::Observe(int type, |
| 236 const content::NotificationSource& source, | 255 const content::NotificationSource& source, |
| 237 const content::NotificationDetails& details) { | 256 const content::NotificationDetails& details) { |
| 238 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); | 257 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); |
| 239 content::RenderProcessHost* rph = | 258 content::RenderProcessHost* rph = |
| 240 content::Source<content::RenderProcessHost>(source).ptr(); | 259 content::Source<content::RenderProcessHost>(source).ptr(); |
| 241 if (rph == process_) | 260 if (rph == process_) |
| 242 process_ = NULL; | 261 process_ = NULL; |
| 243 } | 262 } |
| 244 | 263 |
| 245 void SiteInstance::LockToOrigin() { | 264 void SiteInstanceImpl::LockToOrigin() { |
| 246 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 265 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 247 if (command_line.HasSwitch(switches::kEnableStrictSiteIsolation)) { | 266 if (command_line.HasSwitch(switches::kEnableStrictSiteIsolation)) { |
| 248 ChildProcessSecurityPolicy* policy = | 267 ChildProcessSecurityPolicy* policy = |
| 249 ChildProcessSecurityPolicy::GetInstance(); | 268 ChildProcessSecurityPolicy::GetInstance(); |
| 250 policy->LockToOrigin(process_->GetID(), site_); | 269 policy->LockToOrigin(process_->GetID(), site_); |
| 251 } | 270 } |
| 252 } | 271 } |
| 253 | 272 |
| OLD | NEW |