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/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/browser/web_ui_controller_factory.h" | |
15 #include "content/public/common/content_switches.h" | 16 #include "content/public/common/content_switches.h" |
16 #include "content/public/common/url_constants.h" | 17 #include "content/public/common/url_constants.h" |
17 #include "net/base/registry_controlled_domain.h" | 18 #include "net/base/registry_controlled_domain.h" |
18 | 19 |
20 using content::RenderProcessHost; | |
19 using content::RenderProcessHostImpl; | 21 using content::RenderProcessHostImpl; |
20 using content::SiteInstance; | 22 using content::SiteInstance; |
23 using content::WebUIControllerFactory; | |
21 | 24 |
22 static bool IsURLSameAsAnySiteInstance(const GURL& url) { | 25 static bool IsURLSameAsAnySiteInstance(const GURL& url) { |
23 if (!url.is_valid()) | 26 if (!url.is_valid()) |
24 return false; | 27 return false; |
25 | 28 |
26 // We treat javascript: as the same site as any URL since it is actually | 29 // We treat javascript: as the same site as any URL since it is actually |
27 // a modifier on existing pages. | 30 // a modifier on existing pages. |
28 if (url.SchemeIs(chrome::kJavaScriptScheme)) | 31 if (url.SchemeIs(chrome::kJavaScriptScheme)) |
29 return true; | 32 return true; |
30 | 33 |
(...skipping 26 matching lines...) Expand all Loading... | |
57 if (has_site_) | 60 if (has_site_) |
58 browsing_instance_->UnregisterSiteInstance( | 61 browsing_instance_->UnregisterSiteInstance( |
59 static_cast<SiteInstance*>(this)); | 62 static_cast<SiteInstance*>(this)); |
60 } | 63 } |
61 | 64 |
62 int32 SiteInstanceImpl::GetId() { | 65 int32 SiteInstanceImpl::GetId() { |
63 return id_; | 66 return id_; |
64 } | 67 } |
65 | 68 |
66 bool SiteInstanceImpl::HasProcess() const { | 69 bool SiteInstanceImpl::HasProcess() const { |
67 return (process_ != NULL); | 70 if (process_ != NULL) |
71 return true; | |
72 | |
73 // If we would use process-per-site for this site, also check if there is an | |
74 // existing process that we would use if GetProcess were called. | |
awong
2012/06/27 00:26:54
GetProcess -> GetProcess()
Charlie Reis
2012/06/27 20:53:43
Done.
| |
75 content::BrowserContext* browser_context = | |
76 browsing_instance_->browser_context(); | |
77 if (has_site_ && | |
78 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_) && | |
79 RenderProcessHost::GetProcessHostForSite(browser_context, site_)) { | |
80 return true; | |
81 } | |
82 | |
83 return false; | |
68 } | 84 } |
69 | 85 |
70 content::RenderProcessHost* SiteInstanceImpl::GetProcess() { | 86 RenderProcessHost* SiteInstanceImpl::GetProcess() { |
71 // TODO(erikkay) It would be nice to ensure that the renderer type had been | 87 // TODO(erikkay) It would be nice to ensure that the renderer type had been |
72 // properly set before we get here. The default tab creation case winds up | 88 // properly set before we get here. The default tab creation case winds up |
73 // with no site set at this point, so it will default to TYPE_NORMAL. This | 89 // with no site set at this point, so it will default to TYPE_NORMAL. This |
74 // may not be correct, so we'll wind up potentially creating a process that | 90 // may not be correct, so we'll wind up potentially creating a process that |
75 // we then throw away, or worse sharing a process with the wrong process type. | 91 // we then throw away, or worse sharing a process with the wrong process type. |
76 // See crbug.com/43448. | 92 // See crbug.com/43448. |
77 | 93 |
78 // Create a new process if ours went away or was reused. | 94 // Create a new process if ours went away or was reused. |
79 if (!process_) { | 95 if (!process_) { |
80 // See if we should reuse an old process | 96 content::BrowserContext* browser_context = |
81 if (content::RenderProcessHost::ShouldTryToUseExistingProcessHost( | 97 browsing_instance_->browser_context(); |
82 browsing_instance_->browser_context(), site_)) | 98 |
83 process_ = content::RenderProcessHost::GetExistingProcessHost( | 99 // If we should use process-per-site mode (either in general or for the |
84 browsing_instance_->browser_context(), site_); | 100 // given site), then look for an existing RenderProcessHost for the site. |
101 bool use_process_per_site = has_site_ && | |
102 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_); | |
103 if (use_process_per_site) { | |
104 process_ = RenderProcessHost::GetProcessHostForSite(browser_context, | |
105 site_); | |
106 } | |
107 | |
108 // If not (or if none found), see if we should reuse an existing process. | |
109 if (!process_ && | |
110 RenderProcessHost::ShouldTryToUseExistingProcessHost(browser_context, | |
111 site_)) { | |
112 process_ = RenderProcessHost::GetExistingProcessHost(browser_context, | |
113 site_); | |
114 } | |
85 | 115 |
86 // Otherwise (or if that fails), create a new one. | 116 // Otherwise (or if that fails), create a new one. |
87 if (!process_) { | 117 if (!process_) { |
88 if (render_process_host_factory_) { | 118 if (render_process_host_factory_) { |
89 process_ = render_process_host_factory_->CreateRenderProcessHost( | 119 process_ = render_process_host_factory_->CreateRenderProcessHost( |
90 browsing_instance_->browser_context()); | 120 browser_context); |
91 } else { | 121 } else { |
92 process_ = | 122 process_ = |
93 new RenderProcessHostImpl(browsing_instance_->browser_context(), | 123 new RenderProcessHostImpl(browser_context, |
94 site_.SchemeIs(chrome::kGuestScheme)); | 124 site_.SchemeIs(chrome::kGuestScheme)); |
95 } | 125 } |
96 } | 126 } |
127 DCHECK(process_); | |
awong
2012/06/27 00:26:54
Should this be a CHECK? The code can't continue s
Charlie Reis
2012/06/27 20:53:43
Done.
| |
128 | |
129 // If we are using process-per-site, we need to register this process | |
130 // for the current site so that we can find it again. (If no site is set | |
131 // at this time, we will register it in SetSite.) | |
awong
2012/06/27 00:26:54
SetSite -> SetSite().
Though I understand the par
Charlie Reis
2012/06/27 20:53:43
Line 101 checks has_site_ before calling ShouldUse
| |
132 if (use_process_per_site) { | |
133 RenderProcessHost::RegisterProcessHostForSite(browser_context, process_, | |
134 site_); | |
135 } | |
97 | 136 |
98 content::GetContentClient()->browser()->SiteInstanceGotProcess(this); | 137 content::GetContentClient()->browser()->SiteInstanceGotProcess(this); |
99 | 138 |
100 if (has_site_) | 139 if (has_site_) |
101 LockToOrigin(); | 140 LockToOrigin(); |
102 } | 141 } |
103 DCHECK(process_); | 142 DCHECK(process_); |
104 | 143 |
105 return process_; | 144 return process_; |
106 } | 145 } |
107 | 146 |
108 void SiteInstanceImpl::SetSite(const GURL& url) { | 147 void SiteInstanceImpl::SetSite(const GURL& url) { |
109 // A SiteInstance's site should not change. | 148 // A SiteInstance's site should not change. |
110 // TODO(creis): When following links or script navigations, we can currently | 149 // TODO(creis): When following links or script navigations, we can currently |
111 // render pages from other sites in this SiteInstance. This will eventually | 150 // render pages from other sites in this SiteInstance. This will eventually |
112 // be fixed, but until then, we should still not set the site of a | 151 // be fixed, but until then, we should still not set the site of a |
113 // SiteInstance more than once. | 152 // SiteInstance more than once. |
114 DCHECK(!has_site_); | 153 DCHECK(!has_site_); |
115 | 154 |
116 // Remember that this SiteInstance has been used to load a URL, even if the | 155 // Remember that this SiteInstance has been used to load a URL, even if the |
117 // URL is invalid. | 156 // URL is invalid. |
118 has_site_ = true; | 157 has_site_ = true; |
119 site_ = GetSiteForURL(browsing_instance_->browser_context(), url); | 158 content::BrowserContext* browser_context = |
159 browsing_instance_->browser_context(); | |
160 site_ = GetSiteForURL(browser_context, url); | |
120 | 161 |
121 // Now that we have a site, register it with the BrowsingInstance. This | 162 // Now that we have a site, register it with the BrowsingInstance. This |
122 // ensures that we won't create another SiteInstance for this site within | 163 // ensures that we won't create another SiteInstance for this site within |
123 // the same BrowsingInstance, because all same-site pages within a | 164 // the same BrowsingInstance, because all same-site pages within a |
124 // BrowsingInstance can script each other. | 165 // BrowsingInstance can script each other. |
125 browsing_instance_->RegisterSiteInstance(this); | 166 browsing_instance_->RegisterSiteInstance(this); |
126 | 167 |
127 if (process_) | 168 if (process_) { |
128 LockToOrigin(); | 169 LockToOrigin(); |
170 | |
171 // Ensure the process is registered for this site if necessary. | |
172 if (RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_)) { | |
173 RenderProcessHost::RegisterProcessHostForSite( | |
174 browser_context, process_, site_); | |
175 } | |
176 } | |
129 } | 177 } |
130 | 178 |
131 const GURL& SiteInstanceImpl::GetSite() const { | 179 const GURL& SiteInstanceImpl::GetSite() const { |
132 return site_; | 180 return site_; |
133 } | 181 } |
134 | 182 |
135 bool SiteInstanceImpl::HasSite() const { | 183 bool SiteInstanceImpl::HasSite() const { |
136 return has_site_; | 184 return has_site_; |
137 } | 185 } |
138 | 186 |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
259 content::BrowserContext* browser_context, | 307 content::BrowserContext* browser_context, |
260 const GURL& url) { | 308 const GURL& url) { |
261 return content::GetContentClient()->browser()-> | 309 return content::GetContentClient()->browser()-> |
262 GetEffectiveURL(browser_context, url); | 310 GetEffectiveURL(browser_context, url); |
263 } | 311 } |
264 | 312 |
265 void SiteInstanceImpl::Observe(int type, | 313 void SiteInstanceImpl::Observe(int type, |
266 const content::NotificationSource& source, | 314 const content::NotificationSource& source, |
267 const content::NotificationDetails& details) { | 315 const content::NotificationDetails& details) { |
268 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); | 316 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); |
269 content::RenderProcessHost* rph = | 317 RenderProcessHost* rph = content::Source<RenderProcessHost>(source).ptr(); |
270 content::Source<content::RenderProcessHost>(source).ptr(); | |
271 if (rph == process_) | 318 if (rph == process_) |
272 process_ = NULL; | 319 process_ = NULL; |
273 } | 320 } |
274 | 321 |
275 void SiteInstanceImpl::LockToOrigin() { | 322 void SiteInstanceImpl::LockToOrigin() { |
276 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 323 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
277 if (command_line.HasSwitch(switches::kEnableStrictSiteIsolation)) { | 324 if (command_line.HasSwitch(switches::kEnableStrictSiteIsolation)) { |
278 ChildProcessSecurityPolicyImpl* policy = | 325 ChildProcessSecurityPolicyImpl* policy = |
279 ChildProcessSecurityPolicyImpl::GetInstance(); | 326 ChildProcessSecurityPolicyImpl::GetInstance(); |
280 policy->LockToOrigin(process_->GetID(), site_); | 327 policy->LockToOrigin(process_->GetID(), site_); |
281 } | 328 } |
282 } | 329 } |
283 | 330 |
OLD | NEW |