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