blob: 50672efcee7fe4a7ea432bbfc404ea275ba9d9bb [file] [log] [blame]
[email protected]80a8fad2011-01-29 04:02:381// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]1d8a3d1f2011-02-19 07:11:525#include "content/browser/site_instance.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]39365212011-02-24 01:01:007#include "content/browser/browsing_instance.h"
[email protected]f3b1a082011-11-18 00:34:308#include "content/browser/renderer_host/render_process_host_impl.h"
[email protected]67fc0392011-02-25 02:56:579#include "content/browser/webui/web_ui_factory.h"
[email protected]87f3c082011-10-19 18:07:4410#include "content/public/browser/content_browser_client.h"
[email protected]ad50def52011-10-19 23:17:0711#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1612#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:3013#include "content/public/browser/render_process_host_factory.h"
[email protected]a1d29162011-10-14 17:14:0314#include "content/public/common/url_constants.h"
initial.commit09911bf2008-07-26 23:55:2915#include "net/base/registry_controlled_domain.h"
16
[email protected]2ee0418e2009-06-26 21:00:4317static bool IsURLSameAsAnySiteInstance(const GURL& url) {
18 if (!url.is_valid())
19 return false;
[email protected]0f012df82011-05-19 14:15:2920
[email protected]89f550b2011-06-08 18:34:0321 // We treat javascript: as the same site as any URL since it is actually
22 // a modifier on existing pages.
23 if (url.SchemeIs(chrome::kJavaScriptScheme))
[email protected]0f012df82011-05-19 14:15:2924 return true;
[email protected]0f012df82011-05-19 14:15:2925
26 return
27 content::GetContentClient()->browser()->IsURLSameAsAnySiteInstance(url);
[email protected]2ee0418e2009-06-26 21:00:4328}
29
[email protected]992db4c2011-05-12 15:37:1530int32 SiteInstance::next_site_instance_id_ = 1;
31
[email protected]4566f132009-03-12 01:55:1332SiteInstance::SiteInstance(BrowsingInstance* browsing_instance)
[email protected]992db4c2011-05-12 15:37:1533 : id_(next_site_instance_id_++),
34 browsing_instance_(browsing_instance),
[email protected]4566f132009-03-12 01:55:1335 render_process_host_factory_(NULL),
36 process_(NULL),
37 max_page_id_(-1),
38 has_site_(false) {
39 DCHECK(browsing_instance);
40
[email protected]432115822011-07-10 15:52:2741 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:0742 content::NotificationService::AllBrowserContextsAndSources());
[email protected]4566f132009-03-12 01:55:1343}
44
initial.commit09911bf2008-07-26 23:55:2945SiteInstance::~SiteInstance() {
[email protected]6f371442011-11-09 06:45:4646 content::GetContentClient()->browser()->SiteInstanceDeleting(this);
[email protected]056ad2a2011-07-12 02:13:5547
initial.commit09911bf2008-07-26 23:55:2948 // Now that no one is referencing us, we can safely remove ourselves from
49 // the BrowsingInstance. Any future visits to a page from this site
50 // (within the same BrowsingInstance) can safely create a new SiteInstance.
51 if (has_site_)
52 browsing_instance_->UnregisterSiteInstance(this);
53}
54
[email protected]5ab79b02010-04-26 16:47:1155bool SiteInstance::HasProcess() const {
56 return (process_ != NULL);
57}
58
[email protected]f3b1a082011-11-18 00:34:3059content::RenderProcessHost* SiteInstance::GetProcess() {
[email protected]08c8ec7e2010-07-11 17:14:4860 // TODO(erikkay) It would be nice to ensure that the renderer type had been
61 // properly set before we get here. The default tab creation case winds up
62 // with no site set at this point, so it will default to TYPE_NORMAL. This
63 // may not be correct, so we'll wind up potentially creating a process that
64 // we then throw away, or worse sharing a process with the wrong process type.
65 // See crbug.com/43448.
66
initial.commit09911bf2008-07-26 23:55:2967 // Create a new process if ours went away or was reused.
[email protected]4566f132009-03-12 01:55:1368 if (!process_) {
initial.commit09911bf2008-07-26 23:55:2969 // See if we should reuse an old process
[email protected]f3b1a082011-11-18 00:34:3070 if (content::RenderProcessHost::ShouldTryToUseExistingProcessHost())
71 process_ = content::RenderProcessHost::GetExistingProcessHost(
[email protected]2a5221b2011-09-27 23:07:3172 browsing_instance_->browser_context(), site_);
initial.commit09911bf2008-07-26 23:55:2973
74 // Otherwise (or if that fails), create a new one.
[email protected]4566f132009-03-12 01:55:1375 if (!process_) {
[email protected]a6df5112009-01-21 23:50:1576 if (render_process_host_factory_) {
[email protected]4566f132009-03-12 01:55:1377 process_ = render_process_host_factory_->CreateRenderProcessHost(
[email protected]3d7474ff2011-07-27 17:47:3778 browsing_instance_->browser_context());
[email protected]a6df5112009-01-21 23:50:1579 } else {
[email protected]3d7474ff2011-07-27 17:47:3780 process_ =
[email protected]f3b1a082011-11-18 00:34:3081 new RenderProcessHostImpl(browsing_instance_->browser_context());
[email protected]a6df5112009-01-21 23:50:1582 }
83 }
initial.commit09911bf2008-07-26 23:55:2984
[email protected]6f371442011-11-09 06:45:4685 content::GetContentClient()->browser()->SiteInstanceGotProcess(this);
86
[email protected]ae195eca72011-11-17 21:18:3887 // Make sure the process starts at the right max_page_id, and ensure that
88 // we send an update to the renderer process.
89 process_->UpdateAndSendMaxPageID(max_page_id_);
initial.commit09911bf2008-07-26 23:55:2990 }
[email protected]4566f132009-03-12 01:55:1391 DCHECK(process_);
initial.commit09911bf2008-07-26 23:55:2992
[email protected]4566f132009-03-12 01:55:1393 return process_;
initial.commit09911bf2008-07-26 23:55:2994}
95
96void SiteInstance::SetSite(const GURL& url) {
97 // A SiteInstance's site should not change.
98 // TODO(creis): When following links or script navigations, we can currently
99 // render pages from other sites in this SiteInstance. This will eventually
100 // be fixed, but until then, we should still not set the site of a
101 // SiteInstance more than once.
102 DCHECK(!has_site_);
103
104 // Remember that this SiteInstance has been used to load a URL, even if the
105 // URL is invalid.
106 has_site_ = true;
[email protected]3d7474ff2011-07-27 17:47:37107 site_ = GetSiteForURL(browsing_instance_->browser_context(), url);
initial.commit09911bf2008-07-26 23:55:29108
109 // Now that we have a site, register it with the BrowsingInstance. This
110 // ensures that we won't create another SiteInstance for this site within
111 // the same BrowsingInstance, because all same-site pages within a
112 // BrowsingInstance can script each other.
113 browsing_instance_->RegisterSiteInstance(this);
114}
115
116bool SiteInstance::HasRelatedSiteInstance(const GURL& url) {
117 return browsing_instance_->HasSiteInstance(url);
118}
119
120SiteInstance* SiteInstance::GetRelatedSiteInstance(const GURL& url) {
121 return browsing_instance_->GetSiteInstanceForURL(url);
122}
123
[email protected]d292d8a2011-05-25 03:47:11124bool SiteInstance::HasWrongProcessForURL(const GURL& url) const {
125 // Having no process isn't a problem, since we'll assign it correctly.
126 if (!HasProcess())
127 return false;
128
[email protected]2a5221b2011-09-27 23:07:31129 // If the site URL is an extension (e.g., for hosted apps) but the
[email protected]d292d8a2011-05-25 03:47:11130 // process is not (or vice versa), make sure we notice and fix it.
[email protected]2a5221b2011-09-27 23:07:31131 GURL site_url = GetSiteForURL(browsing_instance_->browser_context(), url);
132 content::ContentBrowserClient* browser =
133 content::GetContentClient()->browser();
134 return !browser->IsSuitableHost(process_, site_url);
[email protected]d292d8a2011-05-25 03:47:11135}
136
initial.commit09911bf2008-07-26 23:55:29137/*static*/
[email protected]3d7474ff2011-07-27 17:47:37138SiteInstance* SiteInstance::CreateSiteInstance(
139 content::BrowserContext* browser_context) {
140 return new SiteInstance(new BrowsingInstance(browser_context));
initial.commit09911bf2008-07-26 23:55:29141}
142
143/*static*/
[email protected]3d7474ff2011-07-27 17:47:37144SiteInstance* SiteInstance::CreateSiteInstanceForURL(
145 content::BrowserContext* browser_context, const GURL& url) {
[email protected]633fbc42009-05-07 23:39:47146 // This BrowsingInstance may be deleted if it returns an existing
147 // SiteInstance.
[email protected]3d7474ff2011-07-27 17:47:37148 scoped_refptr<BrowsingInstance> instance(
149 new BrowsingInstance(browser_context));
[email protected]633fbc42009-05-07 23:39:47150 return instance->GetSiteInstanceForURL(url);
[email protected]d8a96622009-05-07 19:21:16151}
152
153/*static*/
[email protected]3d7474ff2011-07-27 17:47:37154GURL SiteInstance::GetSiteForURL(content::BrowserContext* browser_context,
155 const GURL& real_url) {
156 GURL url = GetEffectiveURL(browser_context, real_url);
[email protected]3a8eecb2010-04-22 23:56:30157
initial.commit09911bf2008-07-26 23:55:29158 // URLs with no host should have an empty site.
159 GURL site;
160
161 // TODO(creis): For many protocols, we should just treat the scheme as the
[email protected]76a010b2008-12-07 23:48:03162 // site, since there is no host. e.g., file:, about:, chrome:
initial.commit09911bf2008-07-26 23:55:29163
164 // If the url has a host, then determine the site.
165 if (url.has_host()) {
[email protected]6705b232008-11-26 00:16:51166 // Only keep the scheme and registered domain as given by GetOrigin. This
167 // may also include a port, which we need to drop.
initial.commit09911bf2008-07-26 23:55:29168 site = url.GetOrigin();
169
[email protected]6705b232008-11-26 00:16:51170 // Remove port, if any.
171 if (site.has_port()) {
172 GURL::Replacements rep;
173 rep.ClearPort();
174 site = site.ReplaceComponents(rep);
175 }
176
initial.commit09911bf2008-07-26 23:55:29177 // If this URL has a registered domain, we only want to remember that part.
178 std::string domain =
[email protected]8ac1a752008-07-31 19:40:37179 net::RegistryControlledDomainService::GetDomainAndRegistry(url);
initial.commit09911bf2008-07-26 23:55:29180 if (!domain.empty()) {
181 GURL::Replacements rep;
182 rep.SetHostStr(domain);
183 site = site.ReplaceComponents(rep);
184 }
185 }
186 return site;
187}
188
189/*static*/
[email protected]3d7474ff2011-07-27 17:47:37190bool SiteInstance::IsSameWebSite(content::BrowserContext* browser_context,
[email protected]3a8eecb2010-04-22 23:56:30191 const GURL& real_url1, const GURL& real_url2) {
[email protected]3d7474ff2011-07-27 17:47:37192 GURL url1 = GetEffectiveURL(browser_context, real_url1);
193 GURL url2 = GetEffectiveURL(browser_context, real_url2);
[email protected]3a8eecb2010-04-22 23:56:30194
initial.commit09911bf2008-07-26 23:55:29195 // We infer web site boundaries based on the registered domain name of the
[email protected]6705b232008-11-26 00:16:51196 // top-level page and the scheme. We do not pay attention to the port if
197 // one is present, because pages served from different ports can still
198 // access each other if they change their document.domain variable.
initial.commit09911bf2008-07-26 23:55:29199
[email protected]2ee0418e2009-06-26 21:00:43200 // Some special URLs will match the site instance of any other URL. This is
201 // done before checking both of them for validity, since we want these URLs
202 // to have the same site instance as even an invalid one.
203 if (IsURLSameAsAnySiteInstance(url1) || IsURLSameAsAnySiteInstance(url2))
initial.commit09911bf2008-07-26 23:55:29204 return true;
205
206 // If either URL is invalid, they aren't part of the same site.
[email protected]2ee0418e2009-06-26 21:00:43207 if (!url1.is_valid() || !url2.is_valid())
initial.commit09911bf2008-07-26 23:55:29208 return false;
initial.commit09911bf2008-07-26 23:55:29209
[email protected]6705b232008-11-26 00:16:51210 // If the schemes differ, they aren't part of the same site.
[email protected]2ee0418e2009-06-26 21:00:43211 if (url1.scheme() != url2.scheme())
initial.commit09911bf2008-07-26 23:55:29212 return false;
initial.commit09911bf2008-07-26 23:55:29213
[email protected]8ac1a752008-07-31 19:40:37214 return net::RegistryControlledDomainService::SameDomainOrHost(url1, url2);
initial.commit09911bf2008-07-26 23:55:29215}
[email protected]4566f132009-03-12 01:55:13216
[email protected]3a8eecb2010-04-22 23:56:30217/*static*/
[email protected]3d7474ff2011-07-27 17:47:37218GURL SiteInstance::GetEffectiveURL(content::BrowserContext* browser_context,
219 const GURL& url) {
220 return content::GetContentClient()->browser()->
221 GetEffectiveURL(browser_context, url);
[email protected]3a8eecb2010-04-22 23:56:30222}
223
[email protected]432115822011-07-10 15:52:27224void SiteInstance::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:53225 const content::NotificationSource& source,
226 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:27227 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED);
[email protected]f3b1a082011-11-18 00:34:30228 content::RenderProcessHost* rph =
229 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]4566f132009-03-12 01:55:13230 if (rph == process_)
231 process_ = NULL;
232}