[email protected] | 72daaa9 | 2012-01-18 13:39:02 | [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] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 5 | #include "content/browser/browsing_instance.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
| 7 | #include "base/command_line.h" |
[email protected] | dec76e80 | 2010-09-23 22:43:53 | [diff] [blame] | 8 | #include "base/logging.h" |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 9 | #include "content/browser/site_instance_impl.h" |
[email protected] | ccb79730 | 2011-12-15 16:55:11 | [diff] [blame] | 10 | #include "content/public/browser/browser_context.h" |
[email protected] | 87f3c08 | 2011-10-19 18:07:44 | [diff] [blame] | 11 | #include "content/public/browser/content_browser_client.h" |
Nick Carter | bf6264a5 | 2018-04-06 02:39:33 | [diff] [blame] | 12 | #include "content/public/browser/site_isolation_policy.h" |
[email protected] | c08950d2 | 2011-10-13 22:20:29 | [diff] [blame] | 13 | #include "content/public/common/content_switches.h" |
[email protected] | a1d2916 | 2011-10-14 17:14:03 | [diff] [blame] | 14 | #include "content/public/common/url_constants.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 16 | namespace content { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 17 | |
Alex Moshchuk | 8e5c195 | 2019-01-15 03:39:50 | [diff] [blame] | 18 | // Start the BrowsingInstance ID counter from 1 to avoid a conflict with the |
| 19 | // invalid BrowsingInstanceId value, which is 0 in its underlying IdType32. |
| 20 | int BrowsingInstance::next_browsing_instance_id_ = 1; |
| 21 | |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 22 | BrowsingInstance::BrowsingInstance(BrowserContext* browser_context) |
[email protected] | d5072a8 | 2014-05-15 05:50:18 | [diff] [blame] | 23 | : browser_context_(browser_context), |
Alex Moshchuk | 8e5c195 | 2019-01-15 03:39:50 | [diff] [blame] | 24 | isolation_context_( |
| 25 | BrowsingInstanceId::FromUnsafeValue(next_browsing_instance_id_++)), |
Nate Chapin | 71da03c | 2019-02-05 01:21:41 | [diff] [blame^] | 26 | active_contents_count_(0u), |
| 27 | default_process_(nullptr) { |
Lukasz Anforowicz | 4726a17 | 2018-10-15 21:25:10 | [diff] [blame] | 28 | DCHECK(browser_context); |
[email protected] | dec76e80 | 2010-09-23 22:43:53 | [diff] [blame] | 29 | } |
| 30 | |
Nate Chapin | 71da03c | 2019-02-05 01:21:41 | [diff] [blame^] | 31 | void BrowsingInstance::RenderProcessHostDestroyed(RenderProcessHost* host) { |
| 32 | DCHECK_EQ(default_process_, host); |
| 33 | // Only clear the default process if the RenderProcessHost object goes away, |
| 34 | // not if the renderer process goes away while the RenderProcessHost remains. |
| 35 | default_process_->RemoveObserver(this); |
| 36 | default_process_ = nullptr; |
| 37 | } |
| 38 | |
| 39 | void BrowsingInstance::SetDefaultProcess(RenderProcessHost* default_process) { |
| 40 | DCHECK(!default_process_); |
| 41 | default_process_ = default_process; |
| 42 | default_process_->AddObserver(this); |
| 43 | } |
| 44 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 45 | bool BrowsingInstance::HasSiteInstance(const GURL& url) { |
Alex Moshchuk | 8e5c195 | 2019-01-15 03:39:50 | [diff] [blame] | 46 | std::string site = |
| 47 | SiteInstanceImpl::GetSiteForURL(browser_context_, isolation_context_, url) |
| 48 | .possibly_invalid_spec(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 49 | |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 50 | return site_instance_map_.find(site) != site_instance_map_.end(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 51 | } |
| 52 | |
dcheng | bccd6b8 | 2016-03-30 16:24:19 | [diff] [blame] | 53 | scoped_refptr<SiteInstanceImpl> BrowsingInstance::GetSiteInstanceForURL( |
| 54 | const GURL& url) { |
Alex Moshchuk | 8e5c195 | 2019-01-15 03:39:50 | [diff] [blame] | 55 | std::string site = |
| 56 | SiteInstanceImpl::GetSiteForURL(browser_context_, isolation_context_, url) |
| 57 | .possibly_invalid_spec(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 58 | |
jdoerrie | 55ec69d | 2018-10-08 13:34:46 | [diff] [blame] | 59 | auto i = site_instance_map_.find(site); |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 60 | if (i != site_instance_map_.end()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 61 | return i->second; |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 62 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 63 | // No current SiteInstance for this site, so let's create one. |
dcheng | bccd6b8 | 2016-03-30 16:24:19 | [diff] [blame] | 64 | scoped_refptr<SiteInstanceImpl> instance = new SiteInstanceImpl(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 65 | |
| 66 | // Set the site of this new SiteInstance, which will register it with us. |
| 67 | instance->SetSite(url); |
| 68 | return instance; |
| 69 | } |
| 70 | |
dcheng | bccd6b8 | 2016-03-30 16:24:19 | [diff] [blame] | 71 | void BrowsingInstance::RegisterSiteInstance(SiteInstanceImpl* site_instance) { |
| 72 | DCHECK(site_instance->browsing_instance_.get() == this); |
| 73 | DCHECK(site_instance->HasSite()); |
nick | d5bbd0b | 2016-03-31 19:52:44 | [diff] [blame] | 74 | |
[email protected] | 77ab1731 | 2012-09-28 15:34:59 | [diff] [blame] | 75 | std::string site = site_instance->GetSiteURL().possibly_invalid_spec(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 76 | |
| 77 | // Only register if we don't have a SiteInstance for this site already. |
| 78 | // It's possible to have two SiteInstances point to the same site if two |
| 79 | // tabs are navigated there at the same time. (We don't call SetSite or |
| 80 | // register them until DidNavigate.) If there is a previously existing |
| 81 | // SiteInstance for this site, we just won't register the new one. |
jdoerrie | 55ec69d | 2018-10-08 13:34:46 | [diff] [blame] | 82 | auto i = site_instance_map_.find(site); |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 83 | if (i == site_instance_map_.end()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 84 | // Not previously registered, so register it. |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 85 | site_instance_map_[site] = site_instance; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
dcheng | bccd6b8 | 2016-03-30 16:24:19 | [diff] [blame] | 89 | void BrowsingInstance::UnregisterSiteInstance(SiteInstanceImpl* site_instance) { |
| 90 | DCHECK(site_instance->browsing_instance_.get() == this); |
| 91 | DCHECK(site_instance->HasSite()); |
[email protected] | 77ab1731 | 2012-09-28 15:34:59 | [diff] [blame] | 92 | std::string site = site_instance->GetSiteURL().possibly_invalid_spec(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 93 | |
| 94 | // Only unregister the SiteInstance if it is the same one that is registered |
| 95 | // for the site. (It might have been an unregistered SiteInstance. See the |
| 96 | // comments in RegisterSiteInstance.) |
jdoerrie | 55ec69d | 2018-10-08 13:34:46 | [diff] [blame] | 97 | auto i = site_instance_map_.find(site); |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 98 | if (i != site_instance_map_.end() && i->second == site_instance) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 99 | // Matches, so erase it. |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 100 | site_instance_map_.erase(i); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 101 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 102 | } |
[email protected] | dec76e80 | 2010-09-23 22:43:53 | [diff] [blame] | 103 | |
Alex Moshchuk | 8e5c195 | 2019-01-15 03:39:50 | [diff] [blame] | 104 | // static |
| 105 | BrowsingInstanceId BrowsingInstance::NextBrowsingInstanceId() { |
| 106 | return BrowsingInstanceId::FromUnsafeValue(next_browsing_instance_id_); |
| 107 | } |
| 108 | |
[email protected] | dec76e80 | 2010-09-23 22:43:53 | [diff] [blame] | 109 | BrowsingInstance::~BrowsingInstance() { |
| 110 | // We should only be deleted when all of the SiteInstances that refer to |
| 111 | // us are gone. |
| 112 | DCHECK(site_instance_map_.empty()); |
[email protected] | d5072a8 | 2014-05-15 05:50:18 | [diff] [blame] | 113 | DCHECK_EQ(0u, active_contents_count_); |
Nate Chapin | 71da03c | 2019-02-05 01:21:41 | [diff] [blame^] | 114 | if (default_process_) |
| 115 | default_process_->RemoveObserver(this); |
[email protected] | dec76e80 | 2010-09-23 22:43:53 | [diff] [blame] | 116 | } |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 117 | |
| 118 | } // namespace content |