Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / content / browser / site_instance_impl.cc
blobcd5ac038b4b1dee1e26d64fc0b0a1996ec8b9e97
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
3 // found in the LICENSE file.
5 #include "content/browser/site_instance_impl.h"
7 #include "content/browser/browsing_instance.h"
8 #include "content/browser/child_process_security_policy_impl.h"
9 #include "content/browser/frame_host/debug_urls.h"
10 #include "content/browser/renderer_host/render_process_host_impl.h"
11 #include "content/browser/storage_partition_impl.h"
12 #include "content/common/site_isolation_policy.h"
13 #include "content/public/browser/content_browser_client.h"
14 #include "content/public/browser/render_process_host_factory.h"
15 #include "content/public/browser/web_ui_controller_factory.h"
16 #include "content/public/common/url_constants.h"
17 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
19 namespace content {
21 const RenderProcessHostFactory*
22 SiteInstanceImpl::g_render_process_host_factory_ = NULL;
23 int32 SiteInstanceImpl::next_site_instance_id_ = 1;
25 SiteInstanceImpl::SiteInstanceImpl(BrowsingInstance* browsing_instance)
26 : id_(next_site_instance_id_++),
27 active_frame_count_(0),
28 browsing_instance_(browsing_instance),
29 process_(NULL),
30 has_site_(false) {
31 DCHECK(browsing_instance);
34 SiteInstanceImpl::~SiteInstanceImpl() {
35 GetContentClient()->browser()->SiteInstanceDeleting(this);
37 if (process_)
38 process_->RemoveObserver(this);
40 // Now that no one is referencing us, we can safely remove ourselves from
41 // the BrowsingInstance. Any future visits to a page from this site
42 // (within the same BrowsingInstance) can safely create a new SiteInstance.
43 if (has_site_)
44 browsing_instance_->UnregisterSiteInstance(
45 static_cast<SiteInstance*>(this));
48 int32 SiteInstanceImpl::GetId() {
49 return id_;
52 bool SiteInstanceImpl::HasProcess() const {
53 if (process_ != NULL)
54 return true;
56 // If we would use process-per-site for this site, also check if there is an
57 // existing process that we would use if GetProcess() were called.
58 BrowserContext* browser_context =
59 browsing_instance_->browser_context();
60 if (has_site_ &&
61 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_) &&
62 RenderProcessHostImpl::GetProcessHostForSite(browser_context, site_)) {
63 return true;
66 return false;
69 RenderProcessHost* SiteInstanceImpl::GetProcess() {
70 // TODO(erikkay) It would be nice to ensure that the renderer type had been
71 // properly set before we get here. The default tab creation case winds up
72 // with no site set at this point, so it will default to TYPE_NORMAL. This
73 // may not be correct, so we'll wind up potentially creating a process that
74 // we then throw away, or worse sharing a process with the wrong process type.
75 // See crbug.com/43448.
77 // Create a new process if ours went away or was reused.
78 if (!process_) {
79 BrowserContext* browser_context = browsing_instance_->browser_context();
81 // If we should use process-per-site mode (either in general or for the
82 // given site), then look for an existing RenderProcessHost for the site.
83 bool use_process_per_site = has_site_ &&
84 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_);
85 if (use_process_per_site) {
86 process_ = RenderProcessHostImpl::GetProcessHostForSite(browser_context,
87 site_);
90 // If not (or if none found), see if we should reuse an existing process.
91 if (!process_ && RenderProcessHostImpl::ShouldTryToUseExistingProcessHost(
92 browser_context, site_)) {
93 process_ = RenderProcessHostImpl::GetExistingProcessHost(browser_context,
94 site_);
97 // Otherwise (or if that fails), create a new one.
98 if (!process_) {
99 if (g_render_process_host_factory_) {
100 process_ = g_render_process_host_factory_->CreateRenderProcessHost(
101 browser_context, this);
102 } else {
103 StoragePartitionImpl* partition =
104 static_cast<StoragePartitionImpl*>(
105 BrowserContext::GetStoragePartition(browser_context, this));
106 process_ = new RenderProcessHostImpl(browser_context,
107 partition,
108 site_.SchemeIs(kGuestScheme));
111 CHECK(process_);
112 process_->AddObserver(this);
114 // If we are using process-per-site, we need to register this process
115 // for the current site so that we can find it again. (If no site is set
116 // at this time, we will register it in SetSite().)
117 if (use_process_per_site) {
118 RenderProcessHostImpl::RegisterProcessHostForSite(browser_context,
119 process_, site_);
122 TRACE_EVENT2("navigation", "SiteInstanceImpl::GetProcess",
123 "site id", id_, "process id", process_->GetID());
124 GetContentClient()->browser()->SiteInstanceGotProcess(this);
126 if (has_site_)
127 LockToOrigin();
129 DCHECK(process_);
131 return process_;
134 void SiteInstanceImpl::SetSite(const GURL& url) {
135 TRACE_EVENT2("navigation", "SiteInstanceImpl::SetSite",
136 "site id", id_, "url", url.possibly_invalid_spec());
137 // A SiteInstance's site should not change.
138 // TODO(creis): When following links or script navigations, we can currently
139 // render pages from other sites in this SiteInstance. This will eventually
140 // be fixed, but until then, we should still not set the site of a
141 // SiteInstance more than once.
142 DCHECK(!has_site_);
144 // Remember that this SiteInstance has been used to load a URL, even if the
145 // URL is invalid.
146 has_site_ = true;
147 BrowserContext* browser_context = browsing_instance_->browser_context();
148 site_ = GetSiteForURL(browser_context, url);
150 // Now that we have a site, register it with the BrowsingInstance. This
151 // ensures that we won't create another SiteInstance for this site within
152 // the same BrowsingInstance, because all same-site pages within a
153 // BrowsingInstance can script each other.
154 browsing_instance_->RegisterSiteInstance(this);
156 if (process_) {
157 LockToOrigin();
159 // Ensure the process is registered for this site if necessary.
160 if (RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_)) {
161 RenderProcessHostImpl::RegisterProcessHostForSite(
162 browser_context, process_, site_);
167 const GURL& SiteInstanceImpl::GetSiteURL() const {
168 return site_;
171 bool SiteInstanceImpl::HasSite() const {
172 return has_site_;
175 bool SiteInstanceImpl::HasRelatedSiteInstance(const GURL& url) {
176 return browsing_instance_->HasSiteInstance(url);
179 SiteInstance* SiteInstanceImpl::GetRelatedSiteInstance(const GURL& url) {
180 return browsing_instance_->GetSiteInstanceForURL(url);
183 bool SiteInstanceImpl::IsRelatedSiteInstance(const SiteInstance* instance) {
184 return browsing_instance_.get() == static_cast<const SiteInstanceImpl*>(
185 instance)->browsing_instance_.get();
188 size_t SiteInstanceImpl::GetRelatedActiveContentsCount() {
189 return browsing_instance_->active_contents_count();
192 bool SiteInstanceImpl::HasWrongProcessForURL(const GURL& url) {
193 // Having no process isn't a problem, since we'll assign it correctly.
194 // Note that HasProcess() may return true if process_ is null, in
195 // process-per-site cases where there's an existing process available.
196 // We want to use such a process in the IsSuitableHost check, so we
197 // may end up assigning process_ in the GetProcess() call below.
198 if (!HasProcess())
199 return false;
201 // If the URL to navigate to can be associated with any site instance,
202 // we want to keep it in the same process.
203 if (IsRendererDebugURL(url))
204 return false;
206 // If the site URL is an extension (e.g., for hosted apps or WebUI) but the
207 // process is not (or vice versa), make sure we notice and fix it.
208 GURL site_url = GetSiteForURL(browsing_instance_->browser_context(), url);
209 return !RenderProcessHostImpl::IsSuitableHost(
210 GetProcess(), browsing_instance_->browser_context(), site_url);
213 bool SiteInstanceImpl::RequiresDedicatedProcess() {
214 if (!has_site_)
215 return false;
216 return SiteIsolationPolicy::DoesSiteRequireDedicatedProcess(site_);
219 void SiteInstanceImpl::IncrementRelatedActiveContentsCount() {
220 browsing_instance_->increment_active_contents_count();
223 void SiteInstanceImpl::DecrementRelatedActiveContentsCount() {
224 browsing_instance_->decrement_active_contents_count();
227 void SiteInstanceImpl::set_render_process_host_factory(
228 const RenderProcessHostFactory* rph_factory) {
229 g_render_process_host_factory_ = rph_factory;
232 BrowserContext* SiteInstanceImpl::GetBrowserContext() const {
233 return browsing_instance_->browser_context();
236 /*static*/
237 SiteInstance* SiteInstance::Create(BrowserContext* browser_context) {
238 return new SiteInstanceImpl(new BrowsingInstance(browser_context));
241 /*static*/
242 SiteInstance* SiteInstance::CreateForURL(BrowserContext* browser_context,
243 const GURL& url) {
244 // This will create a new SiteInstance and BrowsingInstance.
245 scoped_refptr<BrowsingInstance> instance(
246 new BrowsingInstance(browser_context));
247 return instance->GetSiteInstanceForURL(url);
250 /*static*/
251 bool SiteInstance::IsSameWebSite(BrowserContext* browser_context,
252 const GURL& real_src_url,
253 const GURL& real_dest_url) {
254 GURL src_url = SiteInstanceImpl::GetEffectiveURL(browser_context,
255 real_src_url);
256 GURL dest_url = SiteInstanceImpl::GetEffectiveURL(browser_context,
257 real_dest_url);
259 // We infer web site boundaries based on the registered domain name of the
260 // top-level page and the scheme. We do not pay attention to the port if
261 // one is present, because pages served from different ports can still
262 // access each other if they change their document.domain variable.
264 // Some special URLs will match the site instance of any other URL. This is
265 // done before checking both of them for validity, since we want these URLs
266 // to have the same site instance as even an invalid one.
267 if (IsRendererDebugURL(src_url) || IsRendererDebugURL(dest_url))
268 return true;
270 // If either URL is invalid, they aren't part of the same site.
271 if (!src_url.is_valid() || !dest_url.is_valid())
272 return false;
274 // If the destination url is just a blank page, we treat them as part of the
275 // same site.
276 GURL blank_page(url::kAboutBlankURL);
277 if (dest_url == blank_page)
278 return true;
280 // If the schemes differ, they aren't part of the same site.
281 if (src_url.scheme() != dest_url.scheme())
282 return false;
284 return net::registry_controlled_domains::SameDomainOrHost(
285 src_url,
286 dest_url,
287 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
290 /*static*/
291 GURL SiteInstance::GetSiteForURL(BrowserContext* browser_context,
292 const GURL& real_url) {
293 // TODO(fsamuel, creis): For some reason appID is not recognized as a host.
294 if (real_url.SchemeIs(kGuestScheme))
295 return real_url;
297 GURL url = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url);
299 // If the url has a host, then determine the site.
300 if (url.has_host()) {
301 // Only keep the scheme and registered domain as given by GetOrigin. This
302 // may also include a port, which we need to drop.
303 GURL site = url.GetOrigin();
305 // Remove port, if any.
306 if (site.has_port()) {
307 GURL::Replacements rep;
308 rep.ClearPort();
309 site = site.ReplaceComponents(rep);
312 // If this URL has a registered domain, we only want to remember that part.
313 std::string domain =
314 net::registry_controlled_domains::GetDomainAndRegistry(
315 url,
316 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
317 if (!domain.empty()) {
318 GURL::Replacements rep;
319 rep.SetHostStr(domain);
320 site = site.ReplaceComponents(rep);
322 return site;
325 // If there is no host but there is a scheme, return the scheme.
326 // This is useful for cases like file URLs.
327 if (url.has_scheme())
328 return GURL(url.scheme() + ":");
330 // Otherwise the URL should be invalid; return an empty site.
331 DCHECK(!url.is_valid());
332 return GURL();
335 /*static*/
336 GURL SiteInstanceImpl::GetEffectiveURL(BrowserContext* browser_context,
337 const GURL& url) {
338 return GetContentClient()->browser()->
339 GetEffectiveURL(browser_context, url);
342 void SiteInstanceImpl::RenderProcessHostDestroyed(RenderProcessHost* host) {
343 DCHECK_EQ(process_, host);
344 process_->RemoveObserver(this);
345 process_ = NULL;
348 void SiteInstanceImpl::LockToOrigin() {
349 // TODO(nick): When all sites are isolated, this operation provides strong
350 // protection. If only some sites are isolated, we need additional logic to
351 // prevent the non-isolated sites from requesting resources for isolated
352 // sites. https://crbug.com/509125
353 if (SiteIsolationPolicy::DoesSiteRequireDedicatedProcess(site_)) {
354 // Guest processes cannot be locked to its site because guests always have
355 // a fixed SiteInstance. The site of GURLs a guest loads doesn't match that
356 // SiteInstance. So we skip locking the guest process to the site.
357 // TODO(ncarter): Remove this exclusion once we can make origin lock per
358 // RenderFrame routing id.
359 if (site_.SchemeIs(content::kGuestScheme))
360 return;
362 // TODO(creis, nick) https://crbug.com/510588 Chrome UI pages use the same
363 // site (chrome://chrome), so they can't be locked because the site being
364 // loaded doesn't match the SiteInstance.
365 if (site_.SchemeIs(content::kChromeUIScheme))
366 return;
368 // TODO(creis, nick): Until we can handle sites with effective URLs at the
369 // call sites of ChildProcessSecurityPolicy::CanAccessDataForOrigin, we
370 // must give the embedder a chance to exempt some sites to avoid process
371 // kills.
372 if (!GetContentClient()->browser()->ShouldLockToOrigin(
373 browsing_instance_->browser_context(), site_))
374 return;
376 ChildProcessSecurityPolicyImpl* policy =
377 ChildProcessSecurityPolicyImpl::GetInstance();
378 policy->LockToOrigin(process_->GetID(), site_);
382 } // namespace content