OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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.h" |
6 | 6 |
7 #include "content/browser/browsing_instance.h" | 7 #include "content/browser/browsing_instance.h" |
8 #include "content/browser/content_browser_client.h" | 8 #include "content/browser/content_browser_client.h" |
9 #include "content/browser/renderer_host/browser_render_process_host.h" | 9 #include "content/browser/renderer_host/browser_render_process_host.h" |
10 #include "content/browser/webui/web_ui_factory.h" | 10 #include "content/browser/webui/web_ui_factory.h" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 // with no site set at this point, so it will default to TYPE_NORMAL. This | 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 | 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. | 66 // we then throw away, or worse sharing a process with the wrong process type. |
67 // See crbug.com/43448. | 67 // See crbug.com/43448. |
68 | 68 |
69 // Create a new process if ours went away or was reused. | 69 // Create a new process if ours went away or was reused. |
70 if (!process_) { | 70 if (!process_) { |
71 // See if we should reuse an old process | 71 // See if we should reuse an old process |
72 if (RenderProcessHost::ShouldTryToUseExistingProcessHost()) | 72 if (RenderProcessHost::ShouldTryToUseExistingProcessHost()) |
73 process_ = RenderProcessHost::GetExistingProcessHost( | 73 process_ = RenderProcessHost::GetExistingProcessHost( |
74 browsing_instance_->profile(), GetRendererType()); | 74 browsing_instance_->context(), GetRendererType()); |
75 | 75 |
76 // Otherwise (or if that fails), create a new one. | 76 // Otherwise (or if that fails), create a new one. |
77 if (!process_) { | 77 if (!process_) { |
78 if (render_process_host_factory_) { | 78 if (render_process_host_factory_) { |
79 process_ = render_process_host_factory_->CreateRenderProcessHost( | 79 process_ = render_process_host_factory_->CreateRenderProcessHost( |
80 browsing_instance_->profile()); | 80 browsing_instance_->context()); |
81 } else { | 81 } else { |
82 process_ = new BrowserRenderProcessHost(browsing_instance_->profile()); | 82 process_ = new BrowserRenderProcessHost(browsing_instance_->context()); |
83 } | 83 } |
84 } | 84 } |
85 | 85 |
86 // Make sure the process starts at the right max_page_id | 86 // Make sure the process starts at the right max_page_id |
87 process_->UpdateMaxPageID(max_page_id_); | 87 process_->UpdateMaxPageID(max_page_id_); |
88 } | 88 } |
89 DCHECK(process_); | 89 DCHECK(process_); |
90 | 90 |
91 return process_; | 91 return process_; |
92 } | 92 } |
93 | 93 |
94 void SiteInstance::SetSite(const GURL& url) { | 94 void SiteInstance::SetSite(const GURL& url) { |
95 // A SiteInstance's site should not change. | 95 // A SiteInstance's site should not change. |
96 // TODO(creis): When following links or script navigations, we can currently | 96 // TODO(creis): When following links or script navigations, we can currently |
97 // render pages from other sites in this SiteInstance. This will eventually | 97 // render pages from other sites in this SiteInstance. This will eventually |
98 // be fixed, but until then, we should still not set the site of a | 98 // be fixed, but until then, we should still not set the site of a |
99 // SiteInstance more than once. | 99 // SiteInstance more than once. |
100 DCHECK(!has_site_); | 100 DCHECK(!has_site_); |
101 | 101 |
102 // Remember that this SiteInstance has been used to load a URL, even if the | 102 // Remember that this SiteInstance has been used to load a URL, even if the |
103 // URL is invalid. | 103 // URL is invalid. |
104 has_site_ = true; | 104 has_site_ = true; |
105 site_ = GetSiteForURL(browsing_instance_->profile(), url); | 105 site_ = GetSiteForURL(browsing_instance_->context(), url); |
106 | 106 |
107 // Now that we have a site, register it with the BrowsingInstance. This | 107 // Now that we have a site, register it with the BrowsingInstance. This |
108 // ensures that we won't create another SiteInstance for this site within | 108 // ensures that we won't create another SiteInstance for this site within |
109 // the same BrowsingInstance, because all same-site pages within a | 109 // the same BrowsingInstance, because all same-site pages within a |
110 // BrowsingInstance can script each other. | 110 // BrowsingInstance can script each other. |
111 browsing_instance_->RegisterSiteInstance(this); | 111 browsing_instance_->RegisterSiteInstance(this); |
112 } | 112 } |
113 | 113 |
114 bool SiteInstance::HasRelatedSiteInstance(const GURL& url) { | 114 bool SiteInstance::HasRelatedSiteInstance(const GURL& url) { |
115 return browsing_instance_->HasSiteInstance(url); | 115 return browsing_instance_->HasSiteInstance(url); |
116 } | 116 } |
117 | 117 |
118 SiteInstance* SiteInstance::GetRelatedSiteInstance(const GURL& url) { | 118 SiteInstance* SiteInstance::GetRelatedSiteInstance(const GURL& url) { |
119 return browsing_instance_->GetSiteInstanceForURL(url); | 119 return browsing_instance_->GetSiteInstanceForURL(url); |
120 } | 120 } |
121 | 121 |
122 bool SiteInstance::HasWrongProcessForURL(const GURL& url) const { | 122 bool SiteInstance::HasWrongProcessForURL(const GURL& url) const { |
123 // Having no process isn't a problem, since we'll assign it correctly. | 123 // Having no process isn't a problem, since we'll assign it correctly. |
124 if (!HasProcess()) | 124 if (!HasProcess()) |
125 return false; | 125 return false; |
126 | 126 |
127 // If the effective URL is an extension (e.g., for hosted apps) but the | 127 // If the effective URL is an extension (e.g., for hosted apps) but the |
128 // process is not (or vice versa), make sure we notice and fix it. | 128 // process is not (or vice versa), make sure we notice and fix it. |
129 GURL effective_url = GetEffectiveURL(browsing_instance_->profile(), url); | 129 GURL effective_url = GetEffectiveURL(browsing_instance_->context(), url); |
130 return effective_url.SchemeIs(chrome::kExtensionScheme) != | 130 return effective_url.SchemeIs(chrome::kExtensionScheme) != |
131 process_->is_extension_process(); | 131 process_->is_extension_process(); |
132 } | 132 } |
133 | 133 |
134 /*static*/ | 134 /*static*/ |
135 SiteInstance* SiteInstance::CreateSiteInstance(Profile* profile) { | 135 SiteInstance* SiteInstance::CreateSiteInstance( |
136 return new SiteInstance(new BrowsingInstance(profile)); | 136 content::BrowserContext* context) { |
| 137 return new SiteInstance(new BrowsingInstance(context)); |
137 } | 138 } |
138 | 139 |
139 /*static*/ | 140 /*static*/ |
140 SiteInstance* SiteInstance::CreateSiteInstanceForURL(Profile* profile, | 141 SiteInstance* SiteInstance::CreateSiteInstanceForURL( |
141 const GURL& url) { | 142 content::BrowserContext* context, const GURL& url) { |
142 // This BrowsingInstance may be deleted if it returns an existing | 143 // This BrowsingInstance may be deleted if it returns an existing |
143 // SiteInstance. | 144 // SiteInstance. |
144 scoped_refptr<BrowsingInstance> instance(new BrowsingInstance(profile)); | 145 scoped_refptr<BrowsingInstance> instance(new BrowsingInstance(context)); |
145 return instance->GetSiteInstanceForURL(url); | 146 return instance->GetSiteInstanceForURL(url); |
146 } | 147 } |
147 | 148 |
148 /*static*/ | 149 /*static*/ |
149 GURL SiteInstance::GetSiteForURL(Profile* profile, const GURL& real_url) { | 150 GURL SiteInstance::GetSiteForURL(content::BrowserContext* context, |
150 GURL url = GetEffectiveURL(profile, real_url); | 151 const GURL& real_url) { |
| 152 GURL url = GetEffectiveURL(context, real_url); |
151 | 153 |
152 // URLs with no host should have an empty site. | 154 // URLs with no host should have an empty site. |
153 GURL site; | 155 GURL site; |
154 | 156 |
155 // TODO(creis): For many protocols, we should just treat the scheme as the | 157 // TODO(creis): For many protocols, we should just treat the scheme as the |
156 // site, since there is no host. e.g., file:, about:, chrome: | 158 // site, since there is no host. e.g., file:, about:, chrome: |
157 | 159 |
158 // If the url has a host, then determine the site. | 160 // If the url has a host, then determine the site. |
159 if (url.has_host()) { | 161 if (url.has_host()) { |
160 // Only keep the scheme and registered domain as given by GetOrigin. This | 162 // Only keep the scheme and registered domain as given by GetOrigin. This |
(...skipping 13 matching lines...) Expand all Loading... |
174 if (!domain.empty()) { | 176 if (!domain.empty()) { |
175 GURL::Replacements rep; | 177 GURL::Replacements rep; |
176 rep.SetHostStr(domain); | 178 rep.SetHostStr(domain); |
177 site = site.ReplaceComponents(rep); | 179 site = site.ReplaceComponents(rep); |
178 } | 180 } |
179 } | 181 } |
180 return site; | 182 return site; |
181 } | 183 } |
182 | 184 |
183 /*static*/ | 185 /*static*/ |
184 bool SiteInstance::IsSameWebSite(Profile* profile, | 186 bool SiteInstance::IsSameWebSite(content::BrowserContext* context, |
185 const GURL& real_url1, const GURL& real_url2) { | 187 const GURL& real_url1, const GURL& real_url2) { |
186 GURL url1 = GetEffectiveURL(profile, real_url1); | 188 GURL url1 = GetEffectiveURL(context, real_url1); |
187 GURL url2 = GetEffectiveURL(profile, real_url2); | 189 GURL url2 = GetEffectiveURL(context, real_url2); |
188 | 190 |
189 // We infer web site boundaries based on the registered domain name of the | 191 // We infer web site boundaries based on the registered domain name of the |
190 // top-level page and the scheme. We do not pay attention to the port if | 192 // top-level page and the scheme. We do not pay attention to the port if |
191 // one is present, because pages served from different ports can still | 193 // one is present, because pages served from different ports can still |
192 // access each other if they change their document.domain variable. | 194 // access each other if they change their document.domain variable. |
193 | 195 |
194 // Some special URLs will match the site instance of any other URL. This is | 196 // Some special URLs will match the site instance of any other URL. This is |
195 // done before checking both of them for validity, since we want these URLs | 197 // done before checking both of them for validity, since we want these URLs |
196 // to have the same site instance as even an invalid one. | 198 // to have the same site instance as even an invalid one. |
197 if (IsURLSameAsAnySiteInstance(url1) || IsURLSameAsAnySiteInstance(url2)) | 199 if (IsURLSameAsAnySiteInstance(url1) || IsURLSameAsAnySiteInstance(url2)) |
198 return true; | 200 return true; |
199 | 201 |
200 // If either URL is invalid, they aren't part of the same site. | 202 // If either URL is invalid, they aren't part of the same site. |
201 if (!url1.is_valid() || !url2.is_valid()) | 203 if (!url1.is_valid() || !url2.is_valid()) |
202 return false; | 204 return false; |
203 | 205 |
204 // If the schemes differ, they aren't part of the same site. | 206 // If the schemes differ, they aren't part of the same site. |
205 if (url1.scheme() != url2.scheme()) | 207 if (url1.scheme() != url2.scheme()) |
206 return false; | 208 return false; |
207 | 209 |
208 return net::RegistryControlledDomainService::SameDomainOrHost(url1, url2); | 210 return net::RegistryControlledDomainService::SameDomainOrHost(url1, url2); |
209 } | 211 } |
210 | 212 |
211 /*static*/ | 213 /*static*/ |
212 GURL SiteInstance::GetEffectiveURL(Profile* profile, const GURL& url) { | 214 GURL SiteInstance::GetEffectiveURL(content::BrowserContext* context, |
213 return content::GetContentClient()->browser()->GetEffectiveURL(profile, url); | 215 const GURL& url) { |
| 216 return content::GetContentClient()->browser()->GetEffectiveURL(context, url); |
214 } | 217 } |
215 | 218 |
216 /*static*/ | 219 /*static*/ |
217 RenderProcessHost::Type SiteInstance::RendererTypeForURL(const GURL& url) { | 220 RenderProcessHost::Type SiteInstance::RendererTypeForURL(const GURL& url) { |
218 if (!url.is_valid()) | 221 if (!url.is_valid()) |
219 return RenderProcessHost::TYPE_NORMAL; | 222 return RenderProcessHost::TYPE_NORMAL; |
220 | 223 |
221 if (url.SchemeIs(chrome::kExtensionScheme)) | 224 if (url.SchemeIs(chrome::kExtensionScheme)) |
222 return RenderProcessHost::TYPE_EXTENSION; | 225 return RenderProcessHost::TYPE_EXTENSION; |
223 | 226 |
(...skipping 14 matching lines...) Expand all Loading... |
238 } | 241 } |
239 | 242 |
240 void SiteInstance::Observe(int type, | 243 void SiteInstance::Observe(int type, |
241 const NotificationSource& source, | 244 const NotificationSource& source, |
242 const NotificationDetails& details) { | 245 const NotificationDetails& details) { |
243 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); | 246 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); |
244 RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr(); | 247 RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr(); |
245 if (rph == process_) | 248 if (rph == process_) |
246 process_ = NULL; | 249 process_ = NULL; |
247 } | 250 } |
OLD | NEW |