[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] | 3936521 | 2011-02-24 01:01:00 | [diff] [blame] | 7 | #include "content/browser/browsing_instance.h" |
[email protected] | b953542 | 2012-02-09 01:47:59 | [diff] [blame] | 8 | #include "content/browser/child_process_security_policy_impl.h" |
[email protected] | c02f1ba | 2014-02-03 06:53:53 | [diff] [blame] | 9 | #include "content/browser/frame_host/debug_urls.h" |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 10 | #include "content/browser/frame_host/frame_tree_node.h" |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 11 | #include "content/browser/renderer_host/render_process_host_impl.h" |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 12 | #include "content/browser/storage_partition_impl.h" |
nick | d30fd96 | 2015-07-27 21:51:08 | [diff] [blame] | 13 | #include "content/common/site_isolation_policy.h" |
[email protected] | 87f3c08 | 2011-10-19 18:07:44 | [diff] [blame] | 14 | #include "content/public/browser/content_browser_client.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] | a1d2916 | 2011-10-14 17:14:03 | [diff] [blame] | 17 | #include "content/public/common/url_constants.h" |
[email protected] | be28b5f4 | 2012-07-20 11:31:25 | [diff] [blame] | 18 | #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 19 | |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 20 | namespace content { |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 21 | |
[email protected] | 3059caa | 2013-06-06 03:48:41 | [diff] [blame] | 22 | const RenderProcessHostFactory* |
| 23 | SiteInstanceImpl::g_render_process_host_factory_ = NULL; |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 24 | int32_t SiteInstanceImpl::next_site_instance_id_ = 1; |
[email protected] | 992db4c | 2011-05-12 15:37:15 | [diff] [blame] | 25 | |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 26 | SiteInstanceImpl::SiteInstanceImpl(BrowsingInstance* browsing_instance) |
[email protected] | 992db4c | 2011-05-12 15:37:15 | [diff] [blame] | 27 | : id_(next_site_instance_id_++), |
creis | cce56cd | 2014-09-29 22:45:22 | [diff] [blame] | 28 | active_frame_count_(0), |
[email protected] | 992db4c | 2011-05-12 15:37:15 | [diff] [blame] | 29 | browsing_instance_(browsing_instance), |
nick | d5bbd0b | 2016-03-31 19:52:44 | [diff] [blame^] | 30 | process_(nullptr), |
| 31 | has_site_(false), |
| 32 | is_default_subframe_site_instance_(false) { |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 33 | DCHECK(browsing_instance); |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 34 | } |
| 35 | |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 36 | SiteInstanceImpl::~SiteInstanceImpl() { |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 37 | GetContentClient()->browser()->SiteInstanceDeleting(this); |
[email protected] | 056ad2a | 2011-07-12 02:13:55 | [diff] [blame] | 38 | |
[email protected] | 40bfaf3 | 2013-11-19 01:35:43 | [diff] [blame] | 39 | if (process_) |
| 40 | process_->RemoveObserver(this); |
| 41 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 42 | // Now that no one is referencing us, we can safely remove ourselves from |
| 43 | // the BrowsingInstance. Any future visits to a page from this site |
| 44 | // (within the same BrowsingInstance) can safely create a new SiteInstance. |
| 45 | if (has_site_) |
dcheng | bccd6b8 | 2016-03-30 16:24:19 | [diff] [blame] | 46 | browsing_instance_->UnregisterSiteInstance(this); |
| 47 | } |
| 48 | |
| 49 | scoped_refptr<SiteInstanceImpl> SiteInstanceImpl::Create( |
| 50 | BrowserContext* browser_context) { |
| 51 | return make_scoped_refptr( |
| 52 | new SiteInstanceImpl(new BrowsingInstance(browser_context))); |
| 53 | } |
| 54 | |
| 55 | scoped_refptr<SiteInstanceImpl> SiteInstanceImpl::CreateForURL( |
| 56 | BrowserContext* browser_context, |
| 57 | const GURL& url) { |
| 58 | // This will create a new SiteInstance and BrowsingInstance. |
| 59 | scoped_refptr<BrowsingInstance> instance( |
| 60 | new BrowsingInstance(browser_context)); |
| 61 | return instance->GetSiteInstanceForURL(url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 62 | } |
| 63 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 64 | int32_t SiteInstanceImpl::GetId() { |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 65 | return id_; |
| 66 | } |
| 67 | |
| 68 | bool SiteInstanceImpl::HasProcess() const { |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 69 | if (process_ != NULL) |
| 70 | return true; |
| 71 | |
| 72 | // If we would use process-per-site for this site, also check if there is an |
| 73 | // existing process that we would use if GetProcess() were called. |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 74 | BrowserContext* browser_context = |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 75 | browsing_instance_->browser_context(); |
| 76 | if (has_site_ && |
[email protected] | 1ae93fb1 | 2013-06-14 03:38:56 | [diff] [blame] | 77 | RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_) && |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 78 | RenderProcessHostImpl::GetProcessHostForSite(browser_context, site_)) { |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | return false; |
[email protected] | 5ab79b0 | 2010-04-26 16:47:11 | [diff] [blame] | 83 | } |
| 84 | |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 85 | RenderProcessHost* SiteInstanceImpl::GetProcess() { |
[email protected] | 08c8ec7e | 2010-07-11 17:14:48 | [diff] [blame] | 86 | // TODO(erikkay) It would be nice to ensure that the renderer type had been |
| 87 | // properly set before we get here. The default tab creation case winds up |
| 88 | // with no site set at this point, so it will default to TYPE_NORMAL. This |
| 89 | // may not be correct, so we'll wind up potentially creating a process that |
| 90 | // we then throw away, or worse sharing a process with the wrong process type. |
| 91 | // See crbug.com/43448. |
| 92 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 93 | // Create a new process if ours went away or was reused. |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 94 | if (!process_) { |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 95 | BrowserContext* browser_context = browsing_instance_->browser_context(); |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 96 | |
| 97 | // If we should use process-per-site mode (either in general or for the |
| 98 | // given site), then look for an existing RenderProcessHost for the site. |
| 99 | bool use_process_per_site = has_site_ && |
[email protected] | 1ae93fb1 | 2013-06-14 03:38:56 | [diff] [blame] | 100 | RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_); |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 101 | if (use_process_per_site) { |
| 102 | process_ = RenderProcessHostImpl::GetProcessHostForSite(browser_context, |
| 103 | site_); |
| 104 | } |
| 105 | |
| 106 | // If not (or if none found), see if we should reuse an existing process. |
| 107 | if (!process_ && RenderProcessHostImpl::ShouldTryToUseExistingProcessHost( |
| 108 | browser_context, site_)) { |
| 109 | process_ = RenderProcessHostImpl::GetExistingProcessHost(browser_context, |
| 110 | site_); |
| 111 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 112 | |
| 113 | // Otherwise (or if that fails), create a new one. |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 114 | if (!process_) { |
[email protected] | 3059caa | 2013-06-06 03:48:41 | [diff] [blame] | 115 | if (g_render_process_host_factory_) { |
| 116 | process_ = g_render_process_host_factory_->CreateRenderProcessHost( |
[email protected] | f681b07 | 2013-06-06 21:53:12 | [diff] [blame] | 117 | browser_context, this); |
[email protected] | a6df511 | 2009-01-21 23:50:15 | [diff] [blame] | 118 | } else { |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 119 | StoragePartitionImpl* partition = |
| 120 | static_cast<StoragePartitionImpl*>( |
| 121 | BrowserContext::GetStoragePartition(browser_context, this)); |
[email protected] | 6eb1a11e | 2013-10-09 00:54:37 | [diff] [blame] | 122 | process_ = new RenderProcessHostImpl(browser_context, |
| 123 | partition, |
[email protected] | 6eb1a11e | 2013-10-09 00:54:37 | [diff] [blame] | 124 | site_.SchemeIs(kGuestScheme)); |
[email protected] | a6df511 | 2009-01-21 23:50:15 | [diff] [blame] | 125 | } |
| 126 | } |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 127 | CHECK(process_); |
[email protected] | 40bfaf3 | 2013-11-19 01:35:43 | [diff] [blame] | 128 | process_->AddObserver(this); |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 129 | |
| 130 | // If we are using process-per-site, we need to register this process |
| 131 | // for the current site so that we can find it again. (If no site is set |
| 132 | // at this time, we will register it in SetSite().) |
| 133 | if (use_process_per_site) { |
| 134 | RenderProcessHostImpl::RegisterProcessHostForSite(browser_context, |
| 135 | process_, site_); |
| 136 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 137 | |
nasko | b8744d2 | 2014-08-28 17:07:43 | [diff] [blame] | 138 | TRACE_EVENT2("navigation", "SiteInstanceImpl::GetProcess", |
| 139 | "site id", id_, "process id", process_->GetID()); |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 140 | GetContentClient()->browser()->SiteInstanceGotProcess(this); |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 141 | |
[email protected] | 313b80bd | 2011-11-23 03:49:10 | [diff] [blame] | 142 | if (has_site_) |
| 143 | LockToOrigin(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 144 | } |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 145 | DCHECK(process_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 146 | |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 147 | return process_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 148 | } |
| 149 | |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 150 | void SiteInstanceImpl::SetSite(const GURL& url) { |
nasko | b8744d2 | 2014-08-28 17:07:43 | [diff] [blame] | 151 | TRACE_EVENT2("navigation", "SiteInstanceImpl::SetSite", |
| 152 | "site id", id_, "url", url.possibly_invalid_spec()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 153 | // A SiteInstance's site should not change. |
| 154 | // TODO(creis): When following links or script navigations, we can currently |
| 155 | // render pages from other sites in this SiteInstance. This will eventually |
| 156 | // be fixed, but until then, we should still not set the site of a |
| 157 | // SiteInstance more than once. |
| 158 | DCHECK(!has_site_); |
| 159 | |
| 160 | // Remember that this SiteInstance has been used to load a URL, even if the |
| 161 | // URL is invalid. |
| 162 | has_site_ = true; |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 163 | BrowserContext* browser_context = browsing_instance_->browser_context(); |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 164 | site_ = GetSiteForURL(browser_context, url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 165 | |
| 166 | // Now that we have a site, register it with the BrowsingInstance. This |
| 167 | // ensures that we won't create another SiteInstance for this site within |
| 168 | // the same BrowsingInstance, because all same-site pages within a |
| 169 | // BrowsingInstance can script each other. |
| 170 | browsing_instance_->RegisterSiteInstance(this); |
[email protected] | 313b80bd | 2011-11-23 03:49:10 | [diff] [blame] | 171 | |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 172 | if (process_) { |
[email protected] | 313b80bd | 2011-11-23 03:49:10 | [diff] [blame] | 173 | LockToOrigin(); |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 174 | |
| 175 | // Ensure the process is registered for this site if necessary. |
[email protected] | 1ae93fb1 | 2013-06-14 03:38:56 | [diff] [blame] | 176 | if (RenderProcessHost::ShouldUseProcessPerSite(browser_context, site_)) { |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 177 | RenderProcessHostImpl::RegisterProcessHostForSite( |
| 178 | browser_context, process_, site_); |
| 179 | } |
| 180 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 181 | } |
| 182 | |
[email protected] | 77ab1731 | 2012-09-28 15:34:59 | [diff] [blame] | 183 | const GURL& SiteInstanceImpl::GetSiteURL() const { |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 184 | return site_; |
| 185 | } |
| 186 | |
| 187 | bool SiteInstanceImpl::HasSite() const { |
| 188 | return has_site_; |
| 189 | } |
| 190 | |
| 191 | bool SiteInstanceImpl::HasRelatedSiteInstance(const GURL& url) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 192 | return browsing_instance_->HasSiteInstance(url); |
| 193 | } |
| 194 | |
dcheng | bccd6b8 | 2016-03-30 16:24:19 | [diff] [blame] | 195 | scoped_refptr<SiteInstance> SiteInstanceImpl::GetRelatedSiteInstance( |
| 196 | const GURL& url) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 197 | return browsing_instance_->GetSiteInstanceForURL(url); |
| 198 | } |
| 199 | |
[email protected] | 14392a5 | 2012-05-02 20:28:44 | [diff] [blame] | 200 | bool SiteInstanceImpl::IsRelatedSiteInstance(const SiteInstance* instance) { |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 201 | return browsing_instance_.get() == static_cast<const SiteInstanceImpl*>( |
| 202 | instance)->browsing_instance_.get(); |
[email protected] | 14392a5 | 2012-05-02 20:28:44 | [diff] [blame] | 203 | } |
| 204 | |
[email protected] | d5072a8 | 2014-05-15 05:50:18 | [diff] [blame] | 205 | size_t SiteInstanceImpl::GetRelatedActiveContentsCount() { |
| 206 | return browsing_instance_->active_contents_count(); |
| 207 | } |
| 208 | |
[email protected] | f88628d0 | 2012-11-11 17:58:59 | [diff] [blame] | 209 | bool SiteInstanceImpl::HasWrongProcessForURL(const GURL& url) { |
[email protected] | d292d8a | 2011-05-25 03:47:11 | [diff] [blame] | 210 | // Having no process isn't a problem, since we'll assign it correctly. |
[email protected] | f88628d0 | 2012-11-11 17:58:59 | [diff] [blame] | 211 | // Note that HasProcess() may return true if process_ is null, in |
| 212 | // process-per-site cases where there's an existing process available. |
| 213 | // We want to use such a process in the IsSuitableHost check, so we |
| 214 | // may end up assigning process_ in the GetProcess() call below. |
| 215 | if (!HasProcess()) |
[email protected] | d292d8a | 2011-05-25 03:47:11 | [diff] [blame] | 216 | return false; |
| 217 | |
[email protected] | 144a810 | 2012-01-14 01:05:31 | [diff] [blame] | 218 | // If the URL to navigate to can be associated with any site instance, |
| 219 | // we want to keep it in the same process. |
[email protected] | c02f1ba | 2014-02-03 06:53:53 | [diff] [blame] | 220 | if (IsRendererDebugURL(url)) |
[email protected] | 144a810 | 2012-01-14 01:05:31 | [diff] [blame] | 221 | return false; |
| 222 | |
[email protected] | 88aae97 | 2011-12-16 01:14:18 | [diff] [blame] | 223 | // 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] | 224 | // process is not (or vice versa), make sure we notice and fix it. |
[email protected] | 2a5221b | 2011-09-27 23:07:31 | [diff] [blame] | 225 | GURL site_url = GetSiteForURL(browsing_instance_->browser_context(), url); |
[email protected] | 88aae97 | 2011-12-16 01:14:18 | [diff] [blame] | 226 | return !RenderProcessHostImpl::IsSuitableHost( |
[email protected] | f88628d0 | 2012-11-11 17:58:59 | [diff] [blame] | 227 | GetProcess(), browsing_instance_->browser_context(), site_url); |
[email protected] | d292d8a | 2011-05-25 03:47:11 | [diff] [blame] | 228 | } |
| 229 | |
nick | d5bbd0b | 2016-03-31 19:52:44 | [diff] [blame^] | 230 | scoped_refptr<SiteInstanceImpl> |
| 231 | SiteInstanceImpl::GetDefaultSubframeSiteInstance() { |
| 232 | return browsing_instance_->GetDefaultSubframeSiteInstance(); |
| 233 | } |
| 234 | |
nick | d30fd96 | 2015-07-27 21:51:08 | [diff] [blame] | 235 | bool SiteInstanceImpl::RequiresDedicatedProcess() { |
| 236 | if (!has_site_) |
| 237 | return false; |
nick | d5bbd0b | 2016-03-31 19:52:44 | [diff] [blame^] | 238 | |
| 239 | return DoesSiteRequireDedicatedProcess(GetBrowserContext(), site_); |
nick | d30fd96 | 2015-07-27 21:51:08 | [diff] [blame] | 240 | } |
| 241 | |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 242 | void SiteInstanceImpl::IncrementActiveFrameCount() { |
| 243 | active_frame_count_++; |
| 244 | } |
| 245 | |
| 246 | void SiteInstanceImpl::DecrementActiveFrameCount() { |
| 247 | if (--active_frame_count_ == 0) |
| 248 | FOR_EACH_OBSERVER(Observer, observers_, ActiveFrameCountIsZero(this)); |
| 249 | } |
| 250 | |
[email protected] | d5072a8 | 2014-05-15 05:50:18 | [diff] [blame] | 251 | void SiteInstanceImpl::IncrementRelatedActiveContentsCount() { |
| 252 | browsing_instance_->increment_active_contents_count(); |
| 253 | } |
| 254 | |
| 255 | void SiteInstanceImpl::DecrementRelatedActiveContentsCount() { |
| 256 | browsing_instance_->decrement_active_contents_count(); |
| 257 | } |
| 258 | |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 259 | void SiteInstanceImpl::AddObserver(Observer* observer) { |
| 260 | observers_.AddObserver(observer); |
| 261 | } |
| 262 | |
| 263 | void SiteInstanceImpl::RemoveObserver(Observer* observer) { |
| 264 | observers_.RemoveObserver(observer); |
| 265 | } |
| 266 | |
[email protected] | 3059caa | 2013-06-06 03:48:41 | [diff] [blame] | 267 | void SiteInstanceImpl::set_render_process_host_factory( |
| 268 | const RenderProcessHostFactory* rph_factory) { |
| 269 | g_render_process_host_factory_ = rph_factory; |
| 270 | } |
| 271 | |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 272 | BrowserContext* SiteInstanceImpl::GetBrowserContext() const { |
[email protected] | 72daaa9 | 2012-01-18 13:39:02 | [diff] [blame] | 273 | return browsing_instance_->browser_context(); |
| 274 | } |
| 275 | |
nick | cc0d914 | 2015-10-14 16:27:10 | [diff] [blame] | 276 | // static |
dcheng | bccd6b8 | 2016-03-30 16:24:19 | [diff] [blame] | 277 | scoped_refptr<SiteInstance> SiteInstance::Create( |
| 278 | BrowserContext* browser_context) { |
| 279 | return SiteInstanceImpl::Create(browser_context); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 280 | } |
| 281 | |
nick | cc0d914 | 2015-10-14 16:27:10 | [diff] [blame] | 282 | // static |
dcheng | bccd6b8 | 2016-03-30 16:24:19 | [diff] [blame] | 283 | scoped_refptr<SiteInstance> SiteInstance::CreateForURL( |
| 284 | BrowserContext* browser_context, |
| 285 | const GURL& url) { |
| 286 | return SiteInstanceImpl::CreateForURL(browser_context, url); |
[email protected] | d8a9662 | 2009-05-07 19:21:16 | [diff] [blame] | 287 | } |
| 288 | |
nick | cc0d914 | 2015-10-14 16:27:10 | [diff] [blame] | 289 | // static |
[email protected] | 399583b | 2012-12-11 09:33:42 | [diff] [blame] | 290 | bool SiteInstance::IsSameWebSite(BrowserContext* browser_context, |
[email protected] | 855d7d57 | 2014-08-02 11:18:17 | [diff] [blame] | 291 | const GURL& real_src_url, |
| 292 | const GURL& real_dest_url) { |
| 293 | GURL src_url = SiteInstanceImpl::GetEffectiveURL(browser_context, |
| 294 | real_src_url); |
| 295 | GURL dest_url = SiteInstanceImpl::GetEffectiveURL(browser_context, |
| 296 | real_dest_url); |
[email protected] | 399583b | 2012-12-11 09:33:42 | [diff] [blame] | 297 | |
| 298 | // We infer web site boundaries based on the registered domain name of the |
| 299 | // top-level page and the scheme. We do not pay attention to the port if |
| 300 | // one is present, because pages served from different ports can still |
| 301 | // access each other if they change their document.domain variable. |
| 302 | |
| 303 | // Some special URLs will match the site instance of any other URL. This is |
| 304 | // done before checking both of them for validity, since we want these URLs |
| 305 | // to have the same site instance as even an invalid one. |
[email protected] | 855d7d57 | 2014-08-02 11:18:17 | [diff] [blame] | 306 | if (IsRendererDebugURL(src_url) || IsRendererDebugURL(dest_url)) |
[email protected] | 399583b | 2012-12-11 09:33:42 | [diff] [blame] | 307 | return true; |
| 308 | |
| 309 | // If either URL is invalid, they aren't part of the same site. |
[email protected] | 855d7d57 | 2014-08-02 11:18:17 | [diff] [blame] | 310 | if (!src_url.is_valid() || !dest_url.is_valid()) |
[email protected] | 399583b | 2012-12-11 09:33:42 | [diff] [blame] | 311 | return false; |
| 312 | |
[email protected] | 855d7d57 | 2014-08-02 11:18:17 | [diff] [blame] | 313 | // If the destination url is just a blank page, we treat them as part of the |
| 314 | // same site. |
| 315 | GURL blank_page(url::kAboutBlankURL); |
| 316 | if (dest_url == blank_page) |
| 317 | return true; |
| 318 | |
[email protected] | 399583b | 2012-12-11 09:33:42 | [diff] [blame] | 319 | // If the schemes differ, they aren't part of the same site. |
[email protected] | 855d7d57 | 2014-08-02 11:18:17 | [diff] [blame] | 320 | if (src_url.scheme() != dest_url.scheme()) |
[email protected] | 399583b | 2012-12-11 09:33:42 | [diff] [blame] | 321 | return false; |
| 322 | |
[email protected] | ed32c21 | 2013-05-14 20:49:29 | [diff] [blame] | 323 | return net::registry_controlled_domains::SameDomainOrHost( |
[email protected] | 855d7d57 | 2014-08-02 11:18:17 | [diff] [blame] | 324 | src_url, |
| 325 | dest_url, |
[email protected] | aabe179 | 2014-01-30 21:37:46 | [diff] [blame] | 326 | net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
[email protected] | 399583b | 2012-12-11 09:33:42 | [diff] [blame] | 327 | } |
| 328 | |
nick | cc0d914 | 2015-10-14 16:27:10 | [diff] [blame] | 329 | // static |
[email protected] | 399583b | 2012-12-11 09:33:42 | [diff] [blame] | 330 | GURL SiteInstance::GetSiteForURL(BrowserContext* browser_context, |
| 331 | const GURL& real_url) { |
[email protected] | 25a9e8e53 | 2012-06-22 06:16:28 | [diff] [blame] | 332 | // TODO(fsamuel, creis): For some reason appID is not recognized as a host. |
[email protected] | 6eb1a11e | 2013-10-09 00:54:37 | [diff] [blame] | 333 | if (real_url.SchemeIs(kGuestScheme)) |
[email protected] | 25a9e8e53 | 2012-06-22 06:16:28 | [diff] [blame] | 334 | return real_url; |
| 335 | |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 336 | GURL url = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url); |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 337 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 338 | // If the url has a host, then determine the site. |
| 339 | if (url.has_host()) { |
[email protected] | 6705b23 | 2008-11-26 00:16:51 | [diff] [blame] | 340 | // Only keep the scheme and registered domain as given by GetOrigin. This |
| 341 | // may also include a port, which we need to drop. |
creis | f60c2cd | 2014-12-18 00:41:02 | [diff] [blame] | 342 | GURL site = url.GetOrigin(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 343 | |
[email protected] | 6705b23 | 2008-11-26 00:16:51 | [diff] [blame] | 344 | // Remove port, if any. |
| 345 | if (site.has_port()) { |
| 346 | GURL::Replacements rep; |
| 347 | rep.ClearPort(); |
| 348 | site = site.ReplaceComponents(rep); |
| 349 | } |
| 350 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 351 | // If this URL has a registered domain, we only want to remember that part. |
| 352 | std::string domain = |
[email protected] | ed32c21 | 2013-05-14 20:49:29 | [diff] [blame] | 353 | net::registry_controlled_domains::GetDomainAndRegistry( |
| 354 | url, |
[email protected] | aabe179 | 2014-01-30 21:37:46 | [diff] [blame] | 355 | net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 356 | if (!domain.empty()) { |
| 357 | GURL::Replacements rep; |
| 358 | rep.SetHostStr(domain); |
| 359 | site = site.ReplaceComponents(rep); |
| 360 | } |
creis | f60c2cd | 2014-12-18 00:41:02 | [diff] [blame] | 361 | return site; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 362 | } |
creis | f60c2cd | 2014-12-18 00:41:02 | [diff] [blame] | 363 | |
| 364 | // If there is no host but there is a scheme, return the scheme. |
| 365 | // This is useful for cases like file URLs. |
| 366 | if (url.has_scheme()) |
| 367 | return GURL(url.scheme() + ":"); |
| 368 | |
| 369 | // Otherwise the URL should be invalid; return an empty site. |
| 370 | DCHECK(!url.is_valid()); |
| 371 | return GURL(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 372 | } |
| 373 | |
nick | cc0d914 | 2015-10-14 16:27:10 | [diff] [blame] | 374 | // static |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 375 | GURL SiteInstanceImpl::GetEffectiveURL(BrowserContext* browser_context, |
| 376 | const GURL& url) { |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 377 | return GetContentClient()->browser()-> |
[email protected] | 3d7474ff | 2011-07-27 17:47:37 | [diff] [blame] | 378 | GetEffectiveURL(browser_context, url); |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 379 | } |
| 380 | |
nick | cc0d914 | 2015-10-14 16:27:10 | [diff] [blame] | 381 | // static |
| 382 | bool SiteInstanceImpl::DoesSiteRequireDedicatedProcess( |
| 383 | BrowserContext* browser_context, |
| 384 | const GURL& effective_url) { |
| 385 | // If --site-per-process is enabled, site isolation is enabled everywhere. |
| 386 | if (SiteIsolationPolicy::UseDedicatedProcessesForAllSites()) |
| 387 | return true; |
| 388 | |
| 389 | // Let the content embedder enable site isolation for specific URLs. |
| 390 | if (GetContentClient()->IsSupplementarySiteIsolationModeEnabled() && |
| 391 | GetContentClient()->browser()->DoesSiteRequireDedicatedProcess( |
| 392 | browser_context, effective_url)) { |
| 393 | return true; |
| 394 | } |
| 395 | |
| 396 | return false; |
| 397 | } |
| 398 | |
[email protected] | 40bfaf3 | 2013-11-19 01:35:43 | [diff] [blame] | 399 | void SiteInstanceImpl::RenderProcessHostDestroyed(RenderProcessHost* host) { |
| 400 | DCHECK_EQ(process_, host); |
| 401 | process_->RemoveObserver(this); |
nick | d5bbd0b | 2016-03-31 19:52:44 | [diff] [blame^] | 402 | process_ = nullptr; |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 403 | } |
[email protected] | 313b80bd | 2011-11-23 03:49:10 | [diff] [blame] | 404 | |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 405 | void SiteInstanceImpl::RenderProcessWillExit(RenderProcessHost* host) { |
| 406 | // TODO(nick): http://crbug.com/575400 - RenderProcessWillExit might not serve |
| 407 | // any purpose here. |
| 408 | FOR_EACH_OBSERVER(Observer, observers_, RenderProcessGone(this)); |
| 409 | } |
| 410 | |
| 411 | void SiteInstanceImpl::RenderProcessExited(RenderProcessHost* host, |
| 412 | base::TerminationStatus status, |
| 413 | int exit_code) { |
| 414 | FOR_EACH_OBSERVER(Observer, observers_, RenderProcessGone(this)); |
| 415 | } |
| 416 | |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 417 | void SiteInstanceImpl::LockToOrigin() { |
nick | d30fd96 | 2015-07-27 21:51:08 | [diff] [blame] | 418 | // TODO(nick): When all sites are isolated, this operation provides strong |
| 419 | // protection. If only some sites are isolated, we need additional logic to |
| 420 | // prevent the non-isolated sites from requesting resources for isolated |
| 421 | // sites. https://crbug.com/509125 |
nick | cc0d914 | 2015-10-14 16:27:10 | [diff] [blame] | 422 | if (RequiresDedicatedProcess()) { |
lazyboy | f5283a4 | 2015-06-24 21:53:35 | [diff] [blame] | 423 | // Guest processes cannot be locked to its site because guests always have |
| 424 | // a fixed SiteInstance. The site of GURLs a guest loads doesn't match that |
| 425 | // SiteInstance. So we skip locking the guest process to the site. |
| 426 | // TODO(ncarter): Remove this exclusion once we can make origin lock per |
| 427 | // RenderFrame routing id. |
| 428 | if (site_.SchemeIs(content::kGuestScheme)) |
| 429 | return; |
| 430 | |
lfg | 24d08379 | 2015-07-17 20:45:35 | [diff] [blame] | 431 | // TODO(creis, nick) https://crbug.com/510588 Chrome UI pages use the same |
| 432 | // site (chrome://chrome), so they can't be locked because the site being |
| 433 | // loaded doesn't match the SiteInstance. |
| 434 | if (site_.SchemeIs(content::kChromeUIScheme)) |
| 435 | return; |
| 436 | |
creis | e5d6d173 | 2015-08-25 19:47:06 | [diff] [blame] | 437 | // TODO(creis, nick): Until we can handle sites with effective URLs at the |
| 438 | // call sites of ChildProcessSecurityPolicy::CanAccessDataForOrigin, we |
| 439 | // must give the embedder a chance to exempt some sites to avoid process |
| 440 | // kills. |
nick | 7d0984c | 2015-08-29 00:13:46 | [diff] [blame] | 441 | if (!GetContentClient()->browser()->ShouldLockToOrigin( |
| 442 | browsing_instance_->browser_context(), site_)) |
creis | e5d6d173 | 2015-08-25 19:47:06 | [diff] [blame] | 443 | return; |
| 444 | |
[email protected] | b953542 | 2012-02-09 01:47:59 | [diff] [blame] | 445 | ChildProcessSecurityPolicyImpl* policy = |
| 446 | ChildProcessSecurityPolicyImpl::GetInstance(); |
[email protected] | 313b80bd | 2011-11-23 03:49:10 | [diff] [blame] | 447 | policy->LockToOrigin(process_->GetID(), site_); |
| 448 | } |
| 449 | } |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 450 | |
| 451 | } // namespace content |