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