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