[email protected] | 144a810 | 2012-01-14 01:05:31 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 5 | #include "content/browser/site_instance_impl.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
[email protected] | 313b80bd | 2011-11-23 03:49:10 | [diff] [blame] | 7 | #include "base/command_line.h" |
[email protected] | 3936521 | 2011-02-24 01:01:00 | [diff] [blame] | 8 | #include "content/browser/browsing_instance.h" |
[email protected] | b953542 | 2012-02-09 01:47:59 | [diff] [blame] | 9 | #include "content/browser/child_process_security_policy_impl.h" |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 10 | #include "content/browser/renderer_host/render_process_host_impl.h" |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 11 | #include "content/browser/storage_partition_impl.h" |
[email protected] | 87f3c08 | 2011-10-19 18:07:44 | [diff] [blame] | 12 | #include "content/public/browser/content_browser_client.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 13 | #include "content/public/browser/notification_service.h" |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 14 | #include "content/public/browser/notification_types.h" |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 15 | #include "content/public/browser/render_process_host_factory.h" |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 16 | #include "content/public/browser/web_ui_controller_factory.h" |
[email protected] | 313b80bd | 2011-11-23 03:49:10 | [diff] [blame] | 17 | #include "content/public/common/content_switches.h" |
[email protected] | a1d2916 | 2011-10-14 17:14:03 | [diff] [blame] | 18 | #include "content/public/common/url_constants.h" |
[email protected] | be28b5f4 | 2012-07-20 11:31:25 | [diff] [blame] | 19 | #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 20 | |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 21 | namespace content { |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 22 | |
[email protected] | 2ee0418e | 2009-06-26 21:00:43 | [diff] [blame] | 23 | static bool IsURLSameAsAnySiteInstance(const GURL& url) { |
| 24 | if (!url.is_valid()) |
| 25 | return false; |
[email protected] | 0f012df8 | 2011-05-19 14:15:29 | [diff] [blame] | 26 | |
[email protected] | 89f550b | 2011-06-08 18:34:03 | [diff] [blame] | 27 | // We treat javascript: as the same site as any URL since it is actually |
| 28 | // a modifier on existing pages. |
| 29 | if (url.SchemeIs(chrome::kJavaScriptScheme)) |
[email protected] | 0f012df8 | 2011-05-19 14:15:29 | [diff] [blame] | 30 | return true; |
[email protected] | 0f012df8 | 2011-05-19 14:15:29 | [diff] [blame] | 31 | |
[email protected] | 8bf104801 | 2012-02-08 01:22:18 | [diff] [blame] | 32 | return url == GURL(chrome::kChromeUICrashURL) || |
| 33 | url == GURL(chrome::kChromeUIKillURL) || |
| 34 | url == GURL(chrome::kChromeUIHangURL) || |
[email protected] | 50f86bc | 2012-11-01 19:59:13 | [diff] [blame] | 35 | url == GURL(kChromeUIShorthangURL); |
[email protected] | 2ee0418e | 2009-06-26 21:00:43 | [diff] [blame] | 36 | } |
| 37 | |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 38 | int32 SiteInstanceImpl::next_site_instance_id_ = 1; |
[email protected] | 992db4c | 2011-05-12 15:37:15 | [diff] [blame] | 39 | |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 40 | SiteInstanceImpl::SiteInstanceImpl(BrowsingInstance* browsing_instance) |
[email protected] | 992db4c | 2011-05-12 15:37:15 | [diff] [blame] | 41 | : id_(next_site_instance_id_++), |
| 42 | browsing_instance_(browsing_instance), |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 43 | render_process_host_factory_(NULL), |
| 44 | process_(NULL), |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 45 | has_site_(false) { |
| 46 | DCHECK(browsing_instance); |
| 47 | |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 48 | registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 49 | NotificationService::AllBrowserContextsAndSources()); |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 50 | } |
| 51 | |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 52 | SiteInstanceImpl::~SiteInstanceImpl() { |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 53 | GetContentClient()->browser()->SiteInstanceDeleting(this); |
[email protected] | 056ad2a | 2011-07-12 02:13:55 | [diff] [blame] | 54 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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_) |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 59 | browsing_instance_->UnregisterSiteInstance( |
| 60 | static_cast<SiteInstance*>(this)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 61 | } |
| 62 | |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 63 | int32 SiteInstanceImpl::GetId() { |
| 64 | return id_; |
| 65 | } |
| 66 | |
| 67 | bool SiteInstanceImpl::HasProcess() const { |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 68 | if (process_ != NULL) |
| 69 | return true; |
| 70 | |
| 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. |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 73 | BrowserContext* browser_context = |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 74 | browsing_instance_->browser_context(); |
| 75 | if (has_site_ && |
| 76 | RenderProcessHostImpl::ShouldUseProcessPerSite(browser_context, site_) && |
| 77 | RenderProcessHostImpl::GetProcessHostForSite(browser_context, site_)) { |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | return false; |
[email protected] | 5ab79b0 | 2010-04-26 16:47:11 | [diff] [blame] | 82 | } |
| 83 | |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 84 | RenderProcessHost* SiteInstanceImpl::GetProcess() { |
[email protected] | 08c8ec7e | 2010-07-11 17:14:48 | [diff] [blame] | 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. |
| 91 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 92 | // Create a new process if ours went away or was reused. |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 93 | if (!process_) { |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 94 | BrowserContext* browser_context = browsing_instance_->browser_context(); |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 95 | |
| 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 | RenderProcessHostImpl::ShouldUseProcessPerSite(browser_context, site_); |
| 100 | if (use_process_per_site) { |
| 101 | process_ = RenderProcessHostImpl::GetProcessHostForSite(browser_context, |
| 102 | site_); |
| 103 | } |
| 104 | |
| 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_); |
| 110 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 111 | |
| 112 | // Otherwise (or if that fails), create a new one. |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 113 | if (!process_) { |
[email protected] | a6df511 | 2009-01-21 23:50:15 | [diff] [blame] | 114 | if (render_process_host_factory_) { |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 115 | process_ = render_process_host_factory_->CreateRenderProcessHost( |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 116 | browser_context); |
[email protected] | a6df511 | 2009-01-21 23:50:15 | [diff] [blame] | 117 | } else { |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 118 | StoragePartitionImpl* partition = |
| 119 | static_cast<StoragePartitionImpl*>( |
| 120 | BrowserContext::GetStoragePartition(browser_context, this)); |
[email protected] | 3d7474ff | 2011-07-27 17:47:37 | [diff] [blame] | 121 | process_ = |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 122 | new RenderProcessHostImpl(browser_context, partition, |
[email protected] | 25a9e8e53 | 2012-06-22 06:16:28 | [diff] [blame] | 123 | site_.SchemeIs(chrome::kGuestScheme)); |
[email protected] | a6df511 | 2009-01-21 23:50:15 | [diff] [blame] | 124 | } |
| 125 | } |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 126 | CHECK(process_); |
| 127 | |
| 128 | // If we are using process-per-site, we need to register this process |
| 129 | // for the current site so that we can find it again. (If no site is set |
| 130 | // at this time, we will register it in SetSite().) |
| 131 | if (use_process_per_site) { |
| 132 | RenderProcessHostImpl::RegisterProcessHostForSite(browser_context, |
| 133 | process_, site_); |
| 134 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 135 | |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 136 | GetContentClient()->browser()->SiteInstanceGotProcess(this); |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 137 | |
[email protected] | 313b80bd | 2011-11-23 03:49:10 | [diff] [blame] | 138 | if (has_site_) |
| 139 | LockToOrigin(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 140 | } |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 141 | DCHECK(process_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 142 | |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 143 | return process_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 144 | } |
| 145 | |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 146 | void SiteInstanceImpl::SetSite(const GURL& url) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 147 | // A SiteInstance's site should not change. |
| 148 | // TODO(creis): When following links or script navigations, we can currently |
| 149 | // render pages from other sites in this SiteInstance. This will eventually |
| 150 | // be fixed, but until then, we should still not set the site of a |
| 151 | // SiteInstance more than once. |
| 152 | DCHECK(!has_site_); |
| 153 | |
| 154 | // Remember that this SiteInstance has been used to load a URL, even if the |
| 155 | // URL is invalid. |
| 156 | has_site_ = true; |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 157 | BrowserContext* browser_context = browsing_instance_->browser_context(); |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 158 | site_ = GetSiteForURL(browser_context, url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 159 | |
| 160 | // Now that we have a site, register it with the BrowsingInstance. This |
| 161 | // ensures that we won't create another SiteInstance for this site within |
| 162 | // the same BrowsingInstance, because all same-site pages within a |
| 163 | // BrowsingInstance can script each other. |
| 164 | browsing_instance_->RegisterSiteInstance(this); |
[email protected] | 313b80bd | 2011-11-23 03:49:10 | [diff] [blame] | 165 | |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 166 | if (process_) { |
[email protected] | 313b80bd | 2011-11-23 03:49:10 | [diff] [blame] | 167 | LockToOrigin(); |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 168 | |
| 169 | // Ensure the process is registered for this site if necessary. |
| 170 | if (RenderProcessHostImpl::ShouldUseProcessPerSite(browser_context, |
| 171 | site_)) { |
| 172 | RenderProcessHostImpl::RegisterProcessHostForSite( |
| 173 | browser_context, process_, site_); |
| 174 | } |
| 175 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 176 | } |
| 177 | |
[email protected] | 77ab1731 | 2012-09-28 15:34:59 | [diff] [blame] | 178 | const GURL& SiteInstanceImpl::GetSiteURL() const { |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 179 | return site_; |
| 180 | } |
| 181 | |
| 182 | bool SiteInstanceImpl::HasSite() const { |
| 183 | return has_site_; |
| 184 | } |
| 185 | |
| 186 | bool SiteInstanceImpl::HasRelatedSiteInstance(const GURL& url) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 187 | return browsing_instance_->HasSiteInstance(url); |
| 188 | } |
| 189 | |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 190 | SiteInstance* SiteInstanceImpl::GetRelatedSiteInstance(const GURL& url) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 191 | return browsing_instance_->GetSiteInstanceForURL(url); |
| 192 | } |
| 193 | |
[email protected] | 14392a5 | 2012-05-02 20:28:44 | [diff] [blame] | 194 | bool SiteInstanceImpl::IsRelatedSiteInstance(const SiteInstance* instance) { |
| 195 | return browsing_instance_ == |
| 196 | static_cast<const SiteInstanceImpl*>(instance)->browsing_instance_; |
| 197 | } |
| 198 | |
[email protected] | f88628d0 | 2012-11-11 17:58:59 | [diff] [blame^] | 199 | bool SiteInstanceImpl::HasWrongProcessForURL(const GURL& url) { |
[email protected] | d292d8a | 2011-05-25 03:47:11 | [diff] [blame] | 200 | // Having no process isn't a problem, since we'll assign it correctly. |
[email protected] | f88628d0 | 2012-11-11 17:58:59 | [diff] [blame^] | 201 | // Note that HasProcess() may return true if process_ is null, in |
| 202 | // process-per-site cases where there's an existing process available. |
| 203 | // We want to use such a process in the IsSuitableHost check, so we |
| 204 | // may end up assigning process_ in the GetProcess() call below. |
| 205 | if (!HasProcess()) |
[email protected] | d292d8a | 2011-05-25 03:47:11 | [diff] [blame] | 206 | return false; |
| 207 | |
[email protected] | 144a810 | 2012-01-14 01:05:31 | [diff] [blame] | 208 | // If the URL to navigate to can be associated with any site instance, |
| 209 | // we want to keep it in the same process. |
| 210 | if (IsURLSameAsAnySiteInstance(url)) |
| 211 | return false; |
| 212 | |
[email protected] | 88aae97 | 2011-12-16 01:14:18 | [diff] [blame] | 213 | // If the site URL is an extension (e.g., for hosted apps or WebUI) but the |
[email protected] | d292d8a | 2011-05-25 03:47:11 | [diff] [blame] | 214 | // process is not (or vice versa), make sure we notice and fix it. |
[email protected] | 2a5221b | 2011-09-27 23:07:31 | [diff] [blame] | 215 | GURL site_url = GetSiteForURL(browsing_instance_->browser_context(), url); |
[email protected] | 88aae97 | 2011-12-16 01:14:18 | [diff] [blame] | 216 | return !RenderProcessHostImpl::IsSuitableHost( |
[email protected] | f88628d0 | 2012-11-11 17:58:59 | [diff] [blame^] | 217 | GetProcess(), browsing_instance_->browser_context(), site_url); |
[email protected] | d292d8a | 2011-05-25 03:47:11 | [diff] [blame] | 218 | } |
| 219 | |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 220 | BrowserContext* SiteInstanceImpl::GetBrowserContext() const { |
[email protected] | 72daaa9 | 2012-01-18 13:39:02 | [diff] [blame] | 221 | return browsing_instance_->browser_context(); |
| 222 | } |
| 223 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 224 | /*static*/ |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 225 | SiteInstance* SiteInstance::Create(BrowserContext* browser_context) { |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 226 | return new SiteInstanceImpl(new BrowsingInstance(browser_context)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /*static*/ |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 230 | SiteInstance* SiteInstance::CreateForURL(BrowserContext* browser_context, |
| 231 | const GURL& url) { |
[email protected] | 633fbc4 | 2009-05-07 23:39:47 | [diff] [blame] | 232 | // This BrowsingInstance may be deleted if it returns an existing |
| 233 | // SiteInstance. |
[email protected] | 3d7474ff | 2011-07-27 17:47:37 | [diff] [blame] | 234 | scoped_refptr<BrowsingInstance> instance( |
| 235 | new BrowsingInstance(browser_context)); |
[email protected] | 633fbc4 | 2009-05-07 23:39:47 | [diff] [blame] | 236 | return instance->GetSiteInstanceForURL(url); |
[email protected] | d8a9662 | 2009-05-07 19:21:16 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /*static*/ |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 240 | GURL SiteInstanceImpl::GetSiteForURL(BrowserContext* browser_context, |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 241 | const GURL& real_url) { |
[email protected] | 25a9e8e53 | 2012-06-22 06:16:28 | [diff] [blame] | 242 | // TODO(fsamuel, creis): For some reason appID is not recognized as a host. |
| 243 | if (real_url.SchemeIs(chrome::kGuestScheme)) |
| 244 | return real_url; |
| 245 | |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 246 | GURL url = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url); |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 247 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 248 | // URLs with no host should have an empty site. |
| 249 | GURL site; |
| 250 | |
| 251 | // TODO(creis): For many protocols, we should just treat the scheme as the |
[email protected] | 76a010b | 2008-12-07 23:48:03 | [diff] [blame] | 252 | // site, since there is no host. e.g., file:, about:, chrome: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 253 | |
| 254 | // If the url has a host, then determine the site. |
| 255 | if (url.has_host()) { |
[email protected] | 6705b23 | 2008-11-26 00:16:51 | [diff] [blame] | 256 | // Only keep the scheme and registered domain as given by GetOrigin. This |
| 257 | // may also include a port, which we need to drop. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 258 | site = url.GetOrigin(); |
| 259 | |
[email protected] | 6705b23 | 2008-11-26 00:16:51 | [diff] [blame] | 260 | // Remove port, if any. |
| 261 | if (site.has_port()) { |
| 262 | GURL::Replacements rep; |
| 263 | rep.ClearPort(); |
| 264 | site = site.ReplaceComponents(rep); |
| 265 | } |
| 266 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 267 | // If this URL has a registered domain, we only want to remember that part. |
| 268 | std::string domain = |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 269 | net::RegistryControlledDomainService::GetDomainAndRegistry(url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 270 | if (!domain.empty()) { |
| 271 | GURL::Replacements rep; |
| 272 | rep.SetHostStr(domain); |
| 273 | site = site.ReplaceComponents(rep); |
| 274 | } |
| 275 | } |
| 276 | return site; |
| 277 | } |
| 278 | |
| 279 | /*static*/ |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 280 | bool SiteInstance::IsSameWebSite(BrowserContext* browser_context, |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 281 | const GURL& real_url1, |
| 282 | const GURL& real_url2) { |
| 283 | GURL url1 = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url1); |
| 284 | GURL url2 = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url2); |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 285 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 286 | // We infer web site boundaries based on the registered domain name of the |
[email protected] | 6705b23 | 2008-11-26 00:16:51 | [diff] [blame] | 287 | // top-level page and the scheme. We do not pay attention to the port if |
| 288 | // one is present, because pages served from different ports can still |
| 289 | // access each other if they change their document.domain variable. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 290 | |
[email protected] | 2ee0418e | 2009-06-26 21:00:43 | [diff] [blame] | 291 | // Some special URLs will match the site instance of any other URL. This is |
| 292 | // done before checking both of them for validity, since we want these URLs |
| 293 | // to have the same site instance as even an invalid one. |
| 294 | if (IsURLSameAsAnySiteInstance(url1) || IsURLSameAsAnySiteInstance(url2)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 295 | return true; |
| 296 | |
| 297 | // If either URL is invalid, they aren't part of the same site. |
[email protected] | 2ee0418e | 2009-06-26 21:00:43 | [diff] [blame] | 298 | if (!url1.is_valid() || !url2.is_valid()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 299 | return false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 300 | |
[email protected] | 6705b23 | 2008-11-26 00:16:51 | [diff] [blame] | 301 | // If the schemes differ, they aren't part of the same site. |
[email protected] | 2ee0418e | 2009-06-26 21:00:43 | [diff] [blame] | 302 | if (url1.scheme() != url2.scheme()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 303 | return false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 304 | |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 305 | return net::RegistryControlledDomainService::SameDomainOrHost(url1, url2); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 306 | } |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 307 | |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 308 | /*static*/ |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 309 | GURL SiteInstanceImpl::GetEffectiveURL(BrowserContext* browser_context, |
| 310 | const GURL& url) { |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 311 | return GetContentClient()->browser()-> |
[email protected] | 3d7474ff | 2011-07-27 17:47:37 | [diff] [blame] | 312 | GetEffectiveURL(browser_context, url); |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 313 | } |
| 314 | |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 315 | void SiteInstanceImpl::Observe(int type, |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 316 | const NotificationSource& source, |
| 317 | const NotificationDetails& details) { |
| 318 | DCHECK(type == NOTIFICATION_RENDERER_PROCESS_TERMINATED); |
| 319 | RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr(); |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 320 | if (rph == process_) |
| 321 | process_ = NULL; |
| 322 | } |
[email protected] | 313b80bd | 2011-11-23 03:49:10 | [diff] [blame] | 323 | |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 324 | void SiteInstanceImpl::LockToOrigin() { |
[email protected] | 313b80bd | 2011-11-23 03:49:10 | [diff] [blame] | 325 | const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 326 | if (command_line.HasSwitch(switches::kEnableStrictSiteIsolation)) { |
[email protected] | b953542 | 2012-02-09 01:47:59 | [diff] [blame] | 327 | ChildProcessSecurityPolicyImpl* policy = |
| 328 | ChildProcessSecurityPolicyImpl::GetInstance(); |
[email protected] | 313b80bd | 2011-11-23 03:49:10 | [diff] [blame] | 329 | policy->LockToOrigin(process_->GetID(), site_); |
| 330 | } |
| 331 | } |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 332 | |
| 333 | } // namespace content |