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_impl.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_impl.h" | 9 #include "content/browser/child_process_security_policy_impl.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/browser/storage_partition_impl.h" |
11 #include "content/public/browser/content_browser_client.h" | 12 #include "content/public/browser/content_browser_client.h" |
12 #include "content/public/browser/notification_service.h" | 13 #include "content/public/browser/notification_service.h" |
13 #include "content/public/browser/notification_types.h" | 14 #include "content/public/browser/notification_types.h" |
14 #include "content/public/browser/render_process_host_factory.h" | 15 #include "content/public/browser/render_process_host_factory.h" |
15 #include "content/public/browser/web_ui_controller_factory.h" | 16 #include "content/public/browser/web_ui_controller_factory.h" |
16 #include "content/public/common/content_switches.h" | 17 #include "content/public/common/content_switches.h" |
17 #include "content/public/common/url_constants.h" | 18 #include "content/public/common/url_constants.h" |
18 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 19 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
19 | 20 |
| 21 using content::BrowserContext; |
20 using content::RenderProcessHost; | 22 using content::RenderProcessHost; |
21 using content::RenderProcessHostImpl; | 23 using content::RenderProcessHostImpl; |
22 using content::SiteInstance; | 24 using content::SiteInstance; |
| 25 using content::StoragePartitionImpl; |
23 using content::WebUIControllerFactory; | 26 using content::WebUIControllerFactory; |
24 | 27 |
25 static bool IsURLSameAsAnySiteInstance(const GURL& url) { | 28 static bool IsURLSameAsAnySiteInstance(const GURL& url) { |
26 if (!url.is_valid()) | 29 if (!url.is_valid()) |
27 return false; | 30 return false; |
28 | 31 |
29 // We treat javascript: as the same site as any URL since it is actually | 32 // We treat javascript: as the same site as any URL since it is actually |
30 // a modifier on existing pages. | 33 // a modifier on existing pages. |
31 if (url.SchemeIs(chrome::kJavaScriptScheme)) | 34 if (url.SchemeIs(chrome::kJavaScriptScheme)) |
32 return true; | 35 return true; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 RenderProcessHost* SiteInstanceImpl::GetProcess() { | 89 RenderProcessHost* SiteInstanceImpl::GetProcess() { |
87 // TODO(erikkay) It would be nice to ensure that the renderer type had been | 90 // TODO(erikkay) It would be nice to ensure that the renderer type had been |
88 // properly set before we get here. The default tab creation case winds up | 91 // properly set before we get here. The default tab creation case winds up |
89 // with no site set at this point, so it will default to TYPE_NORMAL. This | 92 // with no site set at this point, so it will default to TYPE_NORMAL. This |
90 // may not be correct, so we'll wind up potentially creating a process that | 93 // may not be correct, so we'll wind up potentially creating a process that |
91 // we then throw away, or worse sharing a process with the wrong process type. | 94 // we then throw away, or worse sharing a process with the wrong process type. |
92 // See crbug.com/43448. | 95 // See crbug.com/43448. |
93 | 96 |
94 // Create a new process if ours went away or was reused. | 97 // Create a new process if ours went away or was reused. |
95 if (!process_) { | 98 if (!process_) { |
96 content::BrowserContext* browser_context = | 99 BrowserContext* browser_context = browsing_instance_->browser_context(); |
97 browsing_instance_->browser_context(); | |
98 | 100 |
99 // If we should use process-per-site mode (either in general or for the | 101 // If we should use process-per-site mode (either in general or for the |
100 // given site), then look for an existing RenderProcessHost for the site. | 102 // given site), then look for an existing RenderProcessHost for the site. |
101 bool use_process_per_site = has_site_ && | 103 bool use_process_per_site = has_site_ && |
102 RenderProcessHostImpl::ShouldUseProcessPerSite(browser_context, site_); | 104 RenderProcessHostImpl::ShouldUseProcessPerSite(browser_context, site_); |
103 if (use_process_per_site) { | 105 if (use_process_per_site) { |
104 process_ = RenderProcessHostImpl::GetProcessHostForSite(browser_context, | 106 process_ = RenderProcessHostImpl::GetProcessHostForSite(browser_context, |
105 site_); | 107 site_); |
106 } | 108 } |
107 | 109 |
108 // If not (or if none found), see if we should reuse an existing process. | 110 // If not (or if none found), see if we should reuse an existing process. |
109 if (!process_ && RenderProcessHostImpl::ShouldTryToUseExistingProcessHost( | 111 if (!process_ && RenderProcessHostImpl::ShouldTryToUseExistingProcessHost( |
110 browser_context, site_)) { | 112 browser_context, site_)) { |
111 process_ = RenderProcessHostImpl::GetExistingProcessHost(browser_context, | 113 process_ = RenderProcessHostImpl::GetExistingProcessHost(browser_context, |
112 site_); | 114 site_); |
113 } | 115 } |
114 | 116 |
115 // Otherwise (or if that fails), create a new one. | 117 // Otherwise (or if that fails), create a new one. |
116 if (!process_) { | 118 if (!process_) { |
117 if (render_process_host_factory_) { | 119 if (render_process_host_factory_) { |
118 process_ = render_process_host_factory_->CreateRenderProcessHost( | 120 process_ = render_process_host_factory_->CreateRenderProcessHost( |
119 browser_context); | 121 browser_context); |
120 } else { | 122 } else { |
| 123 StoragePartitionImpl* partition = |
| 124 static_cast<StoragePartitionImpl*>( |
| 125 BrowserContext::GetStoragePartition(browser_context, this)); |
121 process_ = | 126 process_ = |
122 new RenderProcessHostImpl(browser_context, | 127 new RenderProcessHostImpl(browser_context, partition, |
123 site_.SchemeIs(chrome::kGuestScheme)); | 128 site_.SchemeIs(chrome::kGuestScheme)); |
124 } | 129 } |
125 } | 130 } |
126 CHECK(process_); | 131 CHECK(process_); |
127 | 132 |
128 // If we are using process-per-site, we need to register this process | 133 // If we are using process-per-site, we need to register this process |
129 // for the current site so that we can find it again. (If no site is set | 134 // for the current site so that we can find it again. (If no site is set |
130 // at this time, we will register it in SetSite().) | 135 // at this time, we will register it in SetSite().) |
131 if (use_process_per_site) { | 136 if (use_process_per_site) { |
132 RenderProcessHostImpl::RegisterProcessHostForSite(browser_context, | 137 RenderProcessHostImpl::RegisterProcessHostForSite(browser_context, |
(...skipping 14 matching lines...) Expand all Loading... |
147 // A SiteInstance's site should not change. | 152 // A SiteInstance's site should not change. |
148 // TODO(creis): When following links or script navigations, we can currently | 153 // TODO(creis): When following links or script navigations, we can currently |
149 // render pages from other sites in this SiteInstance. This will eventually | 154 // render pages from other sites in this SiteInstance. This will eventually |
150 // be fixed, but until then, we should still not set the site of a | 155 // be fixed, but until then, we should still not set the site of a |
151 // SiteInstance more than once. | 156 // SiteInstance more than once. |
152 DCHECK(!has_site_); | 157 DCHECK(!has_site_); |
153 | 158 |
154 // Remember that this SiteInstance has been used to load a URL, even if the | 159 // Remember that this SiteInstance has been used to load a URL, even if the |
155 // URL is invalid. | 160 // URL is invalid. |
156 has_site_ = true; | 161 has_site_ = true; |
157 content::BrowserContext* browser_context = | 162 BrowserContext* browser_context = browsing_instance_->browser_context(); |
158 browsing_instance_->browser_context(); | |
159 site_ = GetSiteForURL(browser_context, url); | 163 site_ = GetSiteForURL(browser_context, url); |
160 | 164 |
161 // Now that we have a site, register it with the BrowsingInstance. This | 165 // Now that we have a site, register it with the BrowsingInstance. This |
162 // ensures that we won't create another SiteInstance for this site within | 166 // ensures that we won't create another SiteInstance for this site within |
163 // the same BrowsingInstance, because all same-site pages within a | 167 // the same BrowsingInstance, because all same-site pages within a |
164 // BrowsingInstance can script each other. | 168 // BrowsingInstance can script each other. |
165 browsing_instance_->RegisterSiteInstance(this); | 169 browsing_instance_->RegisterSiteInstance(this); |
166 | 170 |
167 if (process_) { | 171 if (process_) { |
168 LockToOrigin(); | 172 LockToOrigin(); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 if (IsURLSameAsAnySiteInstance(url)) | 211 if (IsURLSameAsAnySiteInstance(url)) |
208 return false; | 212 return false; |
209 | 213 |
210 // If the site URL is an extension (e.g., for hosted apps or WebUI) but the | 214 // If the site URL is an extension (e.g., for hosted apps or WebUI) but the |
211 // process is not (or vice versa), make sure we notice and fix it. | 215 // process is not (or vice versa), make sure we notice and fix it. |
212 GURL site_url = GetSiteForURL(browsing_instance_->browser_context(), url); | 216 GURL site_url = GetSiteForURL(browsing_instance_->browser_context(), url); |
213 return !RenderProcessHostImpl::IsSuitableHost( | 217 return !RenderProcessHostImpl::IsSuitableHost( |
214 process_, browsing_instance_->browser_context(), site_url); | 218 process_, browsing_instance_->browser_context(), site_url); |
215 } | 219 } |
216 | 220 |
217 content::BrowserContext* SiteInstanceImpl::GetBrowserContext() const { | 221 BrowserContext* SiteInstanceImpl::GetBrowserContext() const { |
218 return browsing_instance_->browser_context(); | 222 return browsing_instance_->browser_context(); |
219 } | 223 } |
220 | 224 |
221 /*static*/ | 225 /*static*/ |
222 SiteInstance* SiteInstance::Create(content::BrowserContext* browser_context) { | 226 SiteInstance* SiteInstance::Create(BrowserContext* browser_context) { |
223 return new SiteInstanceImpl(new BrowsingInstance(browser_context)); | 227 return new SiteInstanceImpl(new BrowsingInstance(browser_context)); |
224 } | 228 } |
225 | 229 |
226 /*static*/ | 230 /*static*/ |
227 SiteInstance* SiteInstance::CreateForURL( | 231 SiteInstance* SiteInstance::CreateForURL(BrowserContext* browser_context, |
228 content::BrowserContext* browser_context, const GURL& url) { | 232 const GURL& url) { |
229 // This BrowsingInstance may be deleted if it returns an existing | 233 // This BrowsingInstance may be deleted if it returns an existing |
230 // SiteInstance. | 234 // SiteInstance. |
231 scoped_refptr<BrowsingInstance> instance( | 235 scoped_refptr<BrowsingInstance> instance( |
232 new BrowsingInstance(browser_context)); | 236 new BrowsingInstance(browser_context)); |
233 return instance->GetSiteInstanceForURL(url); | 237 return instance->GetSiteInstanceForURL(url); |
234 } | 238 } |
235 | 239 |
236 /*static*/ | 240 /*static*/ |
237 GURL SiteInstanceImpl::GetSiteForURL(content::BrowserContext* browser_context, | 241 GURL SiteInstanceImpl::GetSiteForURL(BrowserContext* browser_context, |
238 const GURL& real_url) { | 242 const GURL& real_url) { |
239 // TODO(fsamuel, creis): For some reason appID is not recognized as a host. | 243 // TODO(fsamuel, creis): For some reason appID is not recognized as a host. |
240 if (real_url.SchemeIs(chrome::kGuestScheme)) | 244 if (real_url.SchemeIs(chrome::kGuestScheme)) |
241 return real_url; | 245 return real_url; |
242 | 246 |
243 GURL url = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url); | 247 GURL url = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url); |
244 | 248 |
245 // URLs with no host should have an empty site. | 249 // URLs with no host should have an empty site. |
246 GURL site; | 250 GURL site; |
247 | 251 |
(...skipping 19 matching lines...) Expand all Loading... |
267 if (!domain.empty()) { | 271 if (!domain.empty()) { |
268 GURL::Replacements rep; | 272 GURL::Replacements rep; |
269 rep.SetHostStr(domain); | 273 rep.SetHostStr(domain); |
270 site = site.ReplaceComponents(rep); | 274 site = site.ReplaceComponents(rep); |
271 } | 275 } |
272 } | 276 } |
273 return site; | 277 return site; |
274 } | 278 } |
275 | 279 |
276 /*static*/ | 280 /*static*/ |
277 bool SiteInstance::IsSameWebSite(content::BrowserContext* browser_context, | 281 bool SiteInstance::IsSameWebSite(BrowserContext* browser_context, |
278 const GURL& real_url1, | 282 const GURL& real_url1, |
279 const GURL& real_url2) { | 283 const GURL& real_url2) { |
280 GURL url1 = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url1); | 284 GURL url1 = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url1); |
281 GURL url2 = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url2); | 285 GURL url2 = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url2); |
282 | 286 |
283 // We infer web site boundaries based on the registered domain name of the | 287 // We infer web site boundaries based on the registered domain name of the |
284 // top-level page and the scheme. We do not pay attention to the port if | 288 // top-level page and the scheme. We do not pay attention to the port if |
285 // one is present, because pages served from different ports can still | 289 // one is present, because pages served from different ports can still |
286 // access each other if they change their document.domain variable. | 290 // access each other if they change their document.domain variable. |
287 | 291 |
288 // Some special URLs will match the site instance of any other URL. This is | 292 // Some special URLs will match the site instance of any other URL. This is |
289 // done before checking both of them for validity, since we want these URLs | 293 // done before checking both of them for validity, since we want these URLs |
290 // to have the same site instance as even an invalid one. | 294 // to have the same site instance as even an invalid one. |
291 if (IsURLSameAsAnySiteInstance(url1) || IsURLSameAsAnySiteInstance(url2)) | 295 if (IsURLSameAsAnySiteInstance(url1) || IsURLSameAsAnySiteInstance(url2)) |
292 return true; | 296 return true; |
293 | 297 |
294 // If either URL is invalid, they aren't part of the same site. | 298 // If either URL is invalid, they aren't part of the same site. |
295 if (!url1.is_valid() || !url2.is_valid()) | 299 if (!url1.is_valid() || !url2.is_valid()) |
296 return false; | 300 return false; |
297 | 301 |
298 // If the schemes differ, they aren't part of the same site. | 302 // If the schemes differ, they aren't part of the same site. |
299 if (url1.scheme() != url2.scheme()) | 303 if (url1.scheme() != url2.scheme()) |
300 return false; | 304 return false; |
301 | 305 |
302 return net::RegistryControlledDomainService::SameDomainOrHost(url1, url2); | 306 return net::RegistryControlledDomainService::SameDomainOrHost(url1, url2); |
303 } | 307 } |
304 | 308 |
305 /*static*/ | 309 /*static*/ |
306 GURL SiteInstanceImpl::GetEffectiveURL( | 310 GURL SiteInstanceImpl::GetEffectiveURL(BrowserContext* browser_context, |
307 content::BrowserContext* browser_context, | 311 const GURL& url) { |
308 const GURL& url) { | |
309 return content::GetContentClient()->browser()-> | 312 return content::GetContentClient()->browser()-> |
310 GetEffectiveURL(browser_context, url); | 313 GetEffectiveURL(browser_context, url); |
311 } | 314 } |
312 | 315 |
313 void SiteInstanceImpl::Observe(int type, | 316 void SiteInstanceImpl::Observe(int type, |
314 const content::NotificationSource& source, | 317 const content::NotificationSource& source, |
315 const content::NotificationDetails& details) { | 318 const content::NotificationDetails& details) { |
316 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); | 319 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); |
317 RenderProcessHost* rph = content::Source<RenderProcessHost>(source).ptr(); | 320 RenderProcessHost* rph = content::Source<RenderProcessHost>(source).ptr(); |
318 if (rph == process_) | 321 if (rph == process_) |
319 process_ = NULL; | 322 process_ = NULL; |
320 } | 323 } |
321 | 324 |
322 void SiteInstanceImpl::LockToOrigin() { | 325 void SiteInstanceImpl::LockToOrigin() { |
323 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 326 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
324 if (command_line.HasSwitch(switches::kEnableStrictSiteIsolation)) { | 327 if (command_line.HasSwitch(switches::kEnableStrictSiteIsolation)) { |
325 ChildProcessSecurityPolicyImpl* policy = | 328 ChildProcessSecurityPolicyImpl* policy = |
326 ChildProcessSecurityPolicyImpl::GetInstance(); | 329 ChildProcessSecurityPolicyImpl::GetInstance(); |
327 policy->LockToOrigin(process_->GetID(), site_); | 330 policy->LockToOrigin(process_->GetID(), site_); |
328 } | 331 } |
329 } | 332 } |
330 | |
OLD | NEW |