IndexedDBFactory now ForceCloses databases.
[chromium-blink-merge.git] / content / browser / site_instance_impl.cc
blob29f5a7830953afc4bf63d81a531ae00a14324dd8
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 "base/command_line.h"
8 #include "content/browser/browsing_instance.h"
9 #include "content/browser/child_process_security_policy_impl.h"
10 #include "content/browser/renderer_host/render_process_host_impl.h"
11 #include "content/browser/storage_partition_impl.h"
12 #include "content/public/browser/content_browser_client.h"
13 #include "content/public/browser/render_process_host_factory.h"
14 #include "content/public/browser/web_ui_controller_factory.h"
15 #include "content/public/common/content_switches.h"
16 #include "content/public/common/url_constants.h"
17 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
19 namespace content {
21 static bool IsURLSameAsAnySiteInstance(const GURL& url) {
22 if (!url.is_valid())
23 return false;
25 // We treat javascript: as the same site as any URL since it is actually
26 // a modifier on existing pages.
27 if (url.SchemeIs(kJavaScriptScheme))
28 return true;
30 return url == GURL(kChromeUICrashURL) ||
31 url == GURL(kChromeUIKillURL) ||
32 url == GURL(kChromeUIHangURL) ||
33 url == GURL(kChromeUIShorthangURL);
36 const RenderProcessHostFactory*
37 SiteInstanceImpl::g_render_process_host_factory_ = NULL;
38 int32 SiteInstanceImpl::next_site_instance_id_ = 1;
40 SiteInstanceImpl::SiteInstanceImpl(BrowsingInstance* browsing_instance)
41 : id_(next_site_instance_id_++),
42 active_view_count_(0),
43 browsing_instance_(browsing_instance),
44 process_(NULL),
45 has_site_(false) {
46 DCHECK(browsing_instance);
49 SiteInstanceImpl::~SiteInstanceImpl() {
50 GetContentClient()->browser()->SiteInstanceDeleting(this);
52 if (process_)
53 process_->RemoveObserver(this);
55 // Now that no one is referencing us, we can safely remove ourselves from
56 // the BrowsingInstance. Any future visits to a page from this site
57 // (within the same BrowsingInstance) can safely create a new SiteInstance.
58 if (has_site_)
59 browsing_instance_->UnregisterSiteInstance(
60 static_cast<SiteInstance*>(this));
63 int32 SiteInstanceImpl::GetId() {
64 return id_;
67 bool SiteInstanceImpl::HasProcess() const {
68 if (process_ != NULL)
69 return true;
71 // If we would use process-per-site for this site, also check if there is an
72 // existing process that we would use if GetProcess() were called.
73 BrowserContext* browser_context =
74 browsing_instance_->browser_context();
75 if (has_site_ &&
76 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_) &&
77 RenderProcessHostImpl::GetProcessHostForSite(browser_context, site_)) {
78 return true;
81 return false;
84 RenderProcessHost* SiteInstanceImpl::GetProcess() {
85 // TODO(erikkay) It would be nice to ensure that the renderer type had been
86 // properly set before we get here. The default tab creation case winds up
87 // with no site set at this point, so it will default to TYPE_NORMAL. This
88 // may not be correct, so we'll wind up potentially creating a process that
89 // we then throw away, or worse sharing a process with the wrong process type.
90 // See crbug.com/43448.
92 // Create a new process if ours went away or was reused.
93 if (!process_) {
94 BrowserContext* browser_context = browsing_instance_->browser_context();
96 // If we should use process-per-site mode (either in general or for the
97 // given site), then look for an existing RenderProcessHost for the site.
98 bool use_process_per_site = has_site_ &&
99 RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_);
100 if (use_process_per_site) {
101 process_ = RenderProcessHostImpl::GetProcessHostForSite(browser_context,
102 site_);
105 // If not (or if none found), see if we should reuse an existing process.
106 if (!process_ && RenderProcessHostImpl::ShouldTryToUseExistingProcessHost(
107 browser_context, site_)) {
108 process_ = RenderProcessHostImpl::GetExistingProcessHost(browser_context,
109 site_);
112 // Otherwise (or if that fails), create a new one.
113 if (!process_) {
114 if (g_render_process_host_factory_) {
115 process_ = g_render_process_host_factory_->CreateRenderProcessHost(
116 browser_context, this);
117 } else {
118 StoragePartitionImpl* partition =
119 static_cast<StoragePartitionImpl*>(
120 BrowserContext::GetStoragePartition(browser_context, this));
121 bool supports_browser_plugin = GetContentClient()->browser()->
122 SupportsBrowserPlugin(browser_context, site_);
123 process_ = new RenderProcessHostImpl(browser_context,
124 partition,
125 supports_browser_plugin,
126 site_.SchemeIs(kGuestScheme));
129 CHECK(process_);
130 process_->AddObserver(this);
132 // If we are using process-per-site, we need to register this process
133 // for the current site so that we can find it again. (If no site is set
134 // at this time, we will register it in SetSite().)
135 if (use_process_per_site) {
136 RenderProcessHostImpl::RegisterProcessHostForSite(browser_context,
137 process_, site_);
140 GetContentClient()->browser()->SiteInstanceGotProcess(this);
142 if (has_site_)
143 LockToOrigin();
145 DCHECK(process_);
147 return process_;
150 void SiteInstanceImpl::SetSite(const GURL& url) {
151 // A SiteInstance's site should not change.
152 // TODO(creis): When following links or script navigations, we can currently
153 // render pages from other sites in this SiteInstance. This will eventually
154 // be fixed, but until then, we should still not set the site of a
155 // SiteInstance more than once.
156 DCHECK(!has_site_);
158 // Remember that this SiteInstance has been used to load a URL, even if the
159 // URL is invalid.
160 has_site_ = true;
161 BrowserContext* browser_context = browsing_instance_->browser_context();
162 site_ = GetSiteForURL(browser_context, url);
164 // Now that we have a site, register it with the BrowsingInstance. This
165 // ensures that we won't create another SiteInstance for this site within
166 // the same BrowsingInstance, because all same-site pages within a
167 // BrowsingInstance can script each other.
168 browsing_instance_->RegisterSiteInstance(this);
170 if (process_) {
171 LockToOrigin();
173 // Ensure the process is registered for this site if necessary.
174 if (RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_)) {
175 RenderProcessHostImpl::RegisterProcessHostForSite(
176 browser_context, process_, site_);
181 const GURL& SiteInstanceImpl::GetSiteURL() const {
182 return site_;
185 bool SiteInstanceImpl::HasSite() const {
186 return has_site_;
189 bool SiteInstanceImpl::HasRelatedSiteInstance(const GURL& url) {
190 return browsing_instance_->HasSiteInstance(url);
193 SiteInstance* SiteInstanceImpl::GetRelatedSiteInstance(const GURL& url) {
194 return browsing_instance_->GetSiteInstanceForURL(url);
197 bool SiteInstanceImpl::IsRelatedSiteInstance(const SiteInstance* instance) {
198 return browsing_instance_.get() == static_cast<const SiteInstanceImpl*>(
199 instance)->browsing_instance_.get();
202 bool SiteInstanceImpl::HasWrongProcessForURL(const GURL& url) {
203 // Having no process isn't a problem, since we'll assign it correctly.
204 // Note that HasProcess() may return true if process_ is null, in
205 // process-per-site cases where there's an existing process available.
206 // We want to use such a process in the IsSuitableHost check, so we
207 // may end up assigning process_ in the GetProcess() call below.
208 if (!HasProcess())
209 return false;
211 // If the URL to navigate to can be associated with any site instance,
212 // we want to keep it in the same process.
213 if (IsURLSameAsAnySiteInstance(url))
214 return false;
216 // If the site URL is an extension (e.g., for hosted apps or WebUI) but the
217 // process is not (or vice versa), make sure we notice and fix it.
218 GURL site_url = GetSiteForURL(browsing_instance_->browser_context(), url);
219 return !RenderProcessHostImpl::IsSuitableHost(
220 GetProcess(), browsing_instance_->browser_context(), site_url);
223 void SiteInstanceImpl::set_render_process_host_factory(
224 const RenderProcessHostFactory* rph_factory) {
225 g_render_process_host_factory_ = rph_factory;
228 BrowserContext* SiteInstanceImpl::GetBrowserContext() const {
229 return browsing_instance_->browser_context();
232 /*static*/
233 SiteInstance* SiteInstance::Create(BrowserContext* browser_context) {
234 return new SiteInstanceImpl(new BrowsingInstance(browser_context));
237 /*static*/
238 SiteInstance* SiteInstance::CreateForURL(BrowserContext* browser_context,
239 const GURL& url) {
240 // This BrowsingInstance may be deleted if it returns an existing
241 // SiteInstance.
242 scoped_refptr<BrowsingInstance> instance(
243 new BrowsingInstance(browser_context));
244 return instance->GetSiteInstanceForURL(url);
247 /*static*/
248 bool SiteInstance::IsSameWebSite(BrowserContext* browser_context,
249 const GURL& real_url1,
250 const GURL& real_url2) {
251 GURL url1 = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url1);
252 GURL url2 = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url2);
254 // We infer web site boundaries based on the registered domain name of the
255 // top-level page and the scheme. We do not pay attention to the port if
256 // one is present, because pages served from different ports can still
257 // access each other if they change their document.domain variable.
259 // Some special URLs will match the site instance of any other URL. This is
260 // done before checking both of them for validity, since we want these URLs
261 // to have the same site instance as even an invalid one.
262 if (IsURLSameAsAnySiteInstance(url1) || IsURLSameAsAnySiteInstance(url2))
263 return true;
265 // If either URL is invalid, they aren't part of the same site.
266 if (!url1.is_valid() || !url2.is_valid())
267 return false;
269 // If the schemes differ, they aren't part of the same site.
270 if (url1.scheme() != url2.scheme())
271 return false;
273 return net::registry_controlled_domains::SameDomainOrHost(
274 url1,
275 url2,
276 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
279 /*static*/
280 GURL SiteInstance::GetSiteForURL(BrowserContext* browser_context,
281 const GURL& real_url) {
282 // TODO(fsamuel, creis): For some reason appID is not recognized as a host.
283 if (real_url.SchemeIs(kGuestScheme))
284 return real_url;
286 GURL url = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url);
288 // URLs with no host should have an empty site.
289 GURL site;
291 // TODO(creis): For many protocols, we should just treat the scheme as the
292 // site, since there is no host. e.g., file:, about:, chrome:
294 // If the url has a host, then determine the site.
295 if (url.has_host()) {
296 // Only keep the scheme and registered domain as given by GetOrigin. This
297 // may also include a port, which we need to drop.
298 site = url.GetOrigin();
300 // Remove port, if any.
301 if (site.has_port()) {
302 GURL::Replacements rep;
303 rep.ClearPort();
304 site = site.ReplaceComponents(rep);
307 // If this URL has a registered domain, we only want to remember that part.
308 std::string domain =
309 net::registry_controlled_domains::GetDomainAndRegistry(
310 url,
311 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
312 if (!domain.empty()) {
313 GURL::Replacements rep;
314 rep.SetHostStr(domain);
315 site = site.ReplaceComponents(rep);
318 return site;
321 /*static*/
322 GURL SiteInstanceImpl::GetEffectiveURL(BrowserContext* browser_context,
323 const GURL& url) {
324 return GetContentClient()->browser()->
325 GetEffectiveURL(browser_context, url);
328 void SiteInstanceImpl::RenderProcessHostDestroyed(RenderProcessHost* host) {
329 DCHECK_EQ(process_, host);
330 process_->RemoveObserver(this);
331 process_ = NULL;
334 void SiteInstanceImpl::LockToOrigin() {
335 // We currently only restrict this process to a particular site if the
336 // --enable-strict-site-isolation or --site-per-process flags are present.
337 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
338 if (command_line.HasSwitch(switches::kEnableStrictSiteIsolation) ||
339 command_line.HasSwitch(switches::kSitePerProcess)) {
340 ChildProcessSecurityPolicyImpl* policy =
341 ChildProcessSecurityPolicyImpl::GetInstance();
342 policy->LockToOrigin(process_->GetID(), site_);
346 } // namespace content