blob: 44a9f2e5d5b4296ff7666abd09a783a7e80807dc [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]1fd1a502011-03-30 16:55:568#include "content/browser/content_browser_client.h"
[email protected]05fcf982011-04-19 00:44:149#include "content/browser/renderer_host/browser_render_process_host.h"
[email protected]67fc0392011-02-25 02:56:5710#include "content/browser/webui/web_ui_factory.h"
[email protected]1fd1a502011-03-30 16:55:5611#include "content/common/content_client.h"
[email protected]0f012df82011-05-19 14:15:2912#include "content/common/notification_service.h"
13#include "content/common/url_constants.h"
initial.commit09911bf2008-07-26 23:55:2914#include "net/base/registry_controlled_domain.h"
15
[email protected]2ee0418e2009-06-26 21:00:4316static bool IsURLSameAsAnySiteInstance(const GURL& url) {
17 if (!url.is_valid())
18 return false;
[email protected]0f012df82011-05-19 14:15:2919
[email protected]89f550b2011-06-08 18:34:0320 // We treat javascript: as the same site as any URL since it is actually
21 // a modifier on existing pages.
22 if (url.SchemeIs(chrome::kJavaScriptScheme))
[email protected]0f012df82011-05-19 14:15:2923 return true;
[email protected]0f012df82011-05-19 14:15:2924
25 return
26 content::GetContentClient()->browser()->IsURLSameAsAnySiteInstance(url);
[email protected]2ee0418e2009-06-26 21:00:4327}
28
[email protected]992db4c2011-05-12 15:37:1529int32 SiteInstance::next_site_instance_id_ = 1;
30
[email protected]4566f132009-03-12 01:55:1331SiteInstance::SiteInstance(BrowsingInstance* browsing_instance)
[email protected]992db4c2011-05-12 15:37:1532 : id_(next_site_instance_id_++),
33 browsing_instance_(browsing_instance),
[email protected]4566f132009-03-12 01:55:1334 render_process_host_factory_(NULL),
35 process_(NULL),
36 max_page_id_(-1),
37 has_site_(false) {
38 DCHECK(browsing_instance);
39
[email protected]432115822011-07-10 15:52:2740 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]5be94df2009-05-22 18:41:3241 NotificationService::AllSources());
[email protected]4566f132009-03-12 01:55:1342}
43
initial.commit09911bf2008-07-26 23:55:2944SiteInstance::~SiteInstance() {
[email protected]056ad2a2011-07-12 02:13:5545 NotificationService::current()->Notify(
46 content::NOTIFICATION_SITE_INSTANCE_DELETED,
47 Source<SiteInstance>(this),
48 NotificationService::NoDetails());
49
initial.commit09911bf2008-07-26 23:55:2950 // Now that no one is referencing us, we can safely remove ourselves from
51 // the BrowsingInstance. Any future visits to a page from this site
52 // (within the same BrowsingInstance) can safely create a new SiteInstance.
53 if (has_site_)
54 browsing_instance_->UnregisterSiteInstance(this);
55}
56
[email protected]5ab79b02010-04-26 16:47:1157bool SiteInstance::HasProcess() const {
58 return (process_ != NULL);
59}
60
initial.commit09911bf2008-07-26 23:55:2961RenderProcessHost* SiteInstance::GetProcess() {
[email protected]08c8ec7e2010-07-11 17:14:4862 // TODO(erikkay) It would be nice to ensure that the renderer type had been
63 // properly set before we get here. The default tab creation case winds up
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
66 // we then throw away, or worse sharing a process with the wrong process type.
67 // See crbug.com/43448.
68
initial.commit09911bf2008-07-26 23:55:2969 // Create a new process if ours went away or was reused.
[email protected]4566f132009-03-12 01:55:1370 if (!process_) {
initial.commit09911bf2008-07-26 23:55:2971 // See if we should reuse an old process
72 if (RenderProcessHost::ShouldTryToUseExistingProcessHost())
[email protected]4566f132009-03-12 01:55:1373 process_ = RenderProcessHost::GetExistingProcessHost(
[email protected]2a5221b2011-09-27 23:07:3174 browsing_instance_->browser_context(), site_);
initial.commit09911bf2008-07-26 23:55:2975
76 // Otherwise (or if that fails), create a new one.
[email protected]4566f132009-03-12 01:55:1377 if (!process_) {
[email protected]a6df5112009-01-21 23:50:1578 if (render_process_host_factory_) {
[email protected]4566f132009-03-12 01:55:1379 process_ = render_process_host_factory_->CreateRenderProcessHost(
[email protected]3d7474ff2011-07-27 17:47:3780 browsing_instance_->browser_context());
[email protected]a6df5112009-01-21 23:50:1581 } else {
[email protected]3d7474ff2011-07-27 17:47:3782 process_ =
83 new BrowserRenderProcessHost(browsing_instance_->browser_context());
[email protected]a6df5112009-01-21 23:50:1584 }
85 }
initial.commit09911bf2008-07-26 23:55:2986
initial.commit09911bf2008-07-26 23:55:2987 // Make sure the process starts at the right max_page_id
[email protected]4566f132009-03-12 01:55:1388 process_->UpdateMaxPageID(max_page_id_);
initial.commit09911bf2008-07-26 23:55:2989 }
[email protected]4566f132009-03-12 01:55:1390 DCHECK(process_);
initial.commit09911bf2008-07-26 23:55:2991
[email protected]4566f132009-03-12 01:55:1392 return process_;
initial.commit09911bf2008-07-26 23:55:2993}
94
95void SiteInstance::SetSite(const GURL& url) {
96 // A SiteInstance's site should not change.
97 // TODO(creis): When following links or script navigations, we can currently
98 // render pages from other sites in this SiteInstance. This will eventually
99 // be fixed, but until then, we should still not set the site of a
100 // SiteInstance more than once.
101 DCHECK(!has_site_);
102
103 // Remember that this SiteInstance has been used to load a URL, even if the
104 // URL is invalid.
105 has_site_ = true;
[email protected]3d7474ff2011-07-27 17:47:37106 site_ = GetSiteForURL(browsing_instance_->browser_context(), url);
initial.commit09911bf2008-07-26 23:55:29107
108 // Now that we have a site, register it with the BrowsingInstance. This
109 // ensures that we won't create another SiteInstance for this site within
110 // the same BrowsingInstance, because all same-site pages within a
111 // BrowsingInstance can script each other.
112 browsing_instance_->RegisterSiteInstance(this);
113}
114
115bool SiteInstance::HasRelatedSiteInstance(const GURL& url) {
116 return browsing_instance_->HasSiteInstance(url);
117}
118
119SiteInstance* SiteInstance::GetRelatedSiteInstance(const GURL& url) {
120 return browsing_instance_->GetSiteInstanceForURL(url);
121}
122
[email protected]d292d8a2011-05-25 03:47:11123bool SiteInstance::HasWrongProcessForURL(const GURL& url) const {
124 // Having no process isn't a problem, since we'll assign it correctly.
125 if (!HasProcess())
126 return false;
127
[email protected]2a5221b2011-09-27 23:07:31128 // If the site URL is an extension (e.g., for hosted apps) but the
[email protected]d292d8a2011-05-25 03:47:11129 // process is not (or vice versa), make sure we notice and fix it.
[email protected]2a5221b2011-09-27 23:07:31130 GURL site_url = GetSiteForURL(browsing_instance_->browser_context(), url);
131 content::ContentBrowserClient* browser =
132 content::GetContentClient()->browser();
133 return !browser->IsSuitableHost(process_, site_url);
[email protected]d292d8a2011-05-25 03:47:11134}
135
initial.commit09911bf2008-07-26 23:55:29136/*static*/
[email protected]3d7474ff2011-07-27 17:47:37137SiteInstance* SiteInstance::CreateSiteInstance(
138 content::BrowserContext* browser_context) {
139 return new SiteInstance(new BrowsingInstance(browser_context));
initial.commit09911bf2008-07-26 23:55:29140}
141
142/*static*/
[email protected]3d7474ff2011-07-27 17:47:37143SiteInstance* SiteInstance::CreateSiteInstanceForURL(
144 content::BrowserContext* browser_context, const GURL& url) {
[email protected]633fbc42009-05-07 23:39:47145 // This BrowsingInstance may be deleted if it returns an existing
146 // SiteInstance.
[email protected]3d7474ff2011-07-27 17:47:37147 scoped_refptr<BrowsingInstance> instance(
148 new BrowsingInstance(browser_context));
[email protected]633fbc42009-05-07 23:39:47149 return instance->GetSiteInstanceForURL(url);
[email protected]d8a96622009-05-07 19:21:16150}
151
152/*static*/
[email protected]3d7474ff2011-07-27 17:47:37153GURL SiteInstance::GetSiteForURL(content::BrowserContext* browser_context,
154 const GURL& real_url) {
155 GURL url = GetEffectiveURL(browser_context, real_url);
[email protected]3a8eecb2010-04-22 23:56:30156
initial.commit09911bf2008-07-26 23:55:29157 // URLs with no host should have an empty site.
158 GURL site;
159
160 // TODO(creis): For many protocols, we should just treat the scheme as the
[email protected]76a010b2008-12-07 23:48:03161 // site, since there is no host. e.g., file:, about:, chrome:
initial.commit09911bf2008-07-26 23:55:29162
163 // If the url has a host, then determine the site.
164 if (url.has_host()) {
[email protected]6705b232008-11-26 00:16:51165 // Only keep the scheme and registered domain as given by GetOrigin. This
166 // may also include a port, which we need to drop.
initial.commit09911bf2008-07-26 23:55:29167 site = url.GetOrigin();
168
[email protected]6705b232008-11-26 00:16:51169 // Remove port, if any.
170 if (site.has_port()) {
171 GURL::Replacements rep;
172 rep.ClearPort();
173 site = site.ReplaceComponents(rep);
174 }
175
initial.commit09911bf2008-07-26 23:55:29176 // If this URL has a registered domain, we only want to remember that part.
177 std::string domain =
[email protected]8ac1a752008-07-31 19:40:37178 net::RegistryControlledDomainService::GetDomainAndRegistry(url);
initial.commit09911bf2008-07-26 23:55:29179 if (!domain.empty()) {
180 GURL::Replacements rep;
181 rep.SetHostStr(domain);
182 site = site.ReplaceComponents(rep);
183 }
184 }
185 return site;
186}
187
188/*static*/
[email protected]3d7474ff2011-07-27 17:47:37189bool SiteInstance::IsSameWebSite(content::BrowserContext* browser_context,
[email protected]3a8eecb2010-04-22 23:56:30190 const GURL& real_url1, const GURL& real_url2) {
[email protected]3d7474ff2011-07-27 17:47:37191 GURL url1 = GetEffectiveURL(browser_context, real_url1);
192 GURL url2 = GetEffectiveURL(browser_context, real_url2);
[email protected]3a8eecb2010-04-22 23:56:30193
initial.commit09911bf2008-07-26 23:55:29194 // We infer web site boundaries based on the registered domain name of the
[email protected]6705b232008-11-26 00:16:51195 // top-level page and the scheme. We do not pay attention to the port if
196 // one is present, because pages served from different ports can still
197 // access each other if they change their document.domain variable.
initial.commit09911bf2008-07-26 23:55:29198
[email protected]2ee0418e2009-06-26 21:00:43199 // Some special URLs will match the site instance of any other URL. This is
200 // done before checking both of them for validity, since we want these URLs
201 // to have the same site instance as even an invalid one.
202 if (IsURLSameAsAnySiteInstance(url1) || IsURLSameAsAnySiteInstance(url2))
initial.commit09911bf2008-07-26 23:55:29203 return true;
204
205 // If either URL is invalid, they aren't part of the same site.
[email protected]2ee0418e2009-06-26 21:00:43206 if (!url1.is_valid() || !url2.is_valid())
initial.commit09911bf2008-07-26 23:55:29207 return false;
initial.commit09911bf2008-07-26 23:55:29208
[email protected]6705b232008-11-26 00:16:51209 // If the schemes differ, they aren't part of the same site.
[email protected]2ee0418e2009-06-26 21:00:43210 if (url1.scheme() != url2.scheme())
initial.commit09911bf2008-07-26 23:55:29211 return false;
initial.commit09911bf2008-07-26 23:55:29212
[email protected]8ac1a752008-07-31 19:40:37213 return net::RegistryControlledDomainService::SameDomainOrHost(url1, url2);
initial.commit09911bf2008-07-26 23:55:29214}
[email protected]4566f132009-03-12 01:55:13215
[email protected]3a8eecb2010-04-22 23:56:30216/*static*/
[email protected]3d7474ff2011-07-27 17:47:37217GURL SiteInstance::GetEffectiveURL(content::BrowserContext* browser_context,
218 const GURL& url) {
219 return content::GetContentClient()->browser()->
220 GetEffectiveURL(browser_context, url);
[email protected]3a8eecb2010-04-22 23:56:30221}
222
[email protected]432115822011-07-10 15:52:27223void SiteInstance::Observe(int type,
[email protected]4566f132009-03-12 01:55:13224 const NotificationSource& source,
225 const NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:27226 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_TERMINATED);
[email protected]4566f132009-03-12 01:55:13227 RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr();
228 if (rph == process_)
229 process_ = NULL;
230}