| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/browsing_instance.h" | 5 #include "content/browser/browsing_instance.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "content/browser/site_instance_impl.h" | 9 #include "content/browser/site_instance_impl.h" |
| 10 #include "content/common/site_isolation_policy.h" | 10 #include "content/common/site_isolation_policy.h" |
| 11 #include "content/public/browser/browser_context.h" | 11 #include "content/public/browser/browser_context.h" |
| 12 #include "content/public/browser/content_browser_client.h" | 12 #include "content/public/browser/content_browser_client.h" |
| 13 #include "content/public/common/content_switches.h" | 13 #include "content/public/common/content_switches.h" |
| 14 #include "content/public/common/url_constants.h" | 14 #include "content/public/common/url_constants.h" |
| 15 | 15 |
| 16 namespace content { | 16 namespace content { |
| 17 | 17 |
| 18 namespace { |
| 19 |
| 20 using BrowsingInstanceList = std::set<BrowsingInstance*>; |
| 21 base::LazyInstance<BrowsingInstanceList>::DestructorAtExit |
| 22 g_browsing_instance_list = LAZY_INSTANCE_INITIALIZER; |
| 23 |
| 24 } // namespace |
| 25 |
| 26 // static |
| 27 scoped_refptr<SiteInstanceImpl> BrowsingInstance::FindSiteInstance( |
| 28 BrowserContext* browser_context, |
| 29 const GURL& url) { |
| 30 for (BrowsingInstance* browsing_instance : g_browsing_instance_list.Get()) { |
| 31 if (browsing_instance->browser_context() == browser_context && |
| 32 browsing_instance->HasSiteInstance(url)) { |
| 33 return browsing_instance->GetSiteInstanceForURL(url); |
| 34 } |
| 35 } |
| 36 std::string site = SiteInstanceImpl::GetSiteForURL(browser_context, url) |
| 37 .possibly_invalid_spec(); |
| 38 for (BrowsingInstance* browsing_instance : g_browsing_instance_list.Get()) { |
| 39 if (browsing_instance->browser_context() != browser_context) |
| 40 continue; |
| 41 if (browsing_instance->site_to_site_instance_map_.find(site) != |
| 42 browsing_instance->site_to_site_instance_map_.end()) { |
| 43 for (SiteInstanceImpl* instance : |
| 44 browsing_instance->site_to_site_instance_map_[site]) { |
| 45 return instance; |
| 46 } |
| 47 } |
| 48 } |
| 49 return nullptr; |
| 50 } |
| 51 |
| 18 BrowsingInstance::BrowsingInstance(BrowserContext* browser_context) | 52 BrowsingInstance::BrowsingInstance(BrowserContext* browser_context) |
| 19 : browser_context_(browser_context), | 53 : browser_context_(browser_context), active_contents_count_(0u) { |
| 20 active_contents_count_(0u) { | 54 g_browsing_instance_list.Get().insert(this); |
| 21 } | 55 } |
| 22 | 56 |
| 23 bool BrowsingInstance::HasSiteInstance(const GURL& url) { | 57 bool BrowsingInstance::HasSiteInstance(const GURL& url) { |
| 24 std::string site = | 58 std::string site = |
| 25 SiteInstanceImpl::GetSiteForURL(browser_context_, url) | 59 SiteInstanceImpl::GetSiteForURL(browser_context_, url) |
| 26 .possibly_invalid_spec(); | 60 .possibly_invalid_spec(); |
| 27 | |
| 28 return site_instance_map_.find(site) != site_instance_map_.end(); | 61 return site_instance_map_.find(site) != site_instance_map_.end(); |
| 29 } | 62 } |
| 30 | 63 |
| 31 scoped_refptr<SiteInstanceImpl> BrowsingInstance::GetSiteInstanceForURL( | 64 scoped_refptr<SiteInstanceImpl> BrowsingInstance::GetSiteInstanceForURL( |
| 32 const GURL& url) { | 65 const GURL& url) { |
| 33 std::string site = SiteInstanceImpl::GetSiteForURL(browser_context_, url) | 66 std::string site = SiteInstanceImpl::GetSiteForURL(browser_context_, url) |
| 34 .possibly_invalid_spec(); | 67 .possibly_invalid_spec(); |
| 35 | 68 |
| 36 SiteInstanceMap::iterator i = site_instance_map_.find(site); | 69 SiteInstanceMap::iterator i = site_instance_map_.find(site); |
| 37 if (i != site_instance_map_.end()) | 70 if (i != site_instance_map_.end()) |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 // comments in RegisterSiteInstance.) | 128 // comments in RegisterSiteInstance.) |
| 96 SiteInstanceMap::iterator i = site_instance_map_.find(site); | 129 SiteInstanceMap::iterator i = site_instance_map_.find(site); |
| 97 if (i != site_instance_map_.end() && i->second == site_instance) { | 130 if (i != site_instance_map_.end() && i->second == site_instance) { |
| 98 // Matches, so erase it. | 131 // Matches, so erase it. |
| 99 site_instance_map_.erase(i); | 132 site_instance_map_.erase(i); |
| 100 } | 133 } |
| 101 if (default_subframe_site_instance_ == site_instance) | 134 if (default_subframe_site_instance_ == site_instance) |
| 102 default_subframe_site_instance_ = nullptr; | 135 default_subframe_site_instance_ = nullptr; |
| 103 } | 136 } |
| 104 | 137 |
| 138 void BrowsingInstance::RegisterUsedFor(SiteInstanceImpl* site_instance, |
| 139 std::string site) { |
| 140 auto instances = site_to_site_instance_map_.find(site); |
| 141 if (instances == site_to_site_instance_map_.end()) |
| 142 site_to_site_instance_map_[site] = std::set<SiteInstanceImpl*>(); |
| 143 site_to_site_instance_map_[site].insert(site_instance); |
| 144 } |
| 145 |
| 146 void BrowsingInstance::UnregisterUsedFor(SiteInstanceImpl* site_instance, |
| 147 const std::set<std::string>& sites) { |
| 148 for (const auto& site : sites) { |
| 149 if (site_to_site_instance_map_.find(site) != |
| 150 site_to_site_instance_map_.end()) { |
| 151 site_to_site_instance_map_[site].erase(site_instance); |
| 152 if (site_to_site_instance_map_[site].empty()) |
| 153 site_to_site_instance_map_.erase(site); |
| 154 } |
| 155 } |
| 156 } |
| 157 |
| 105 BrowsingInstance::~BrowsingInstance() { | 158 BrowsingInstance::~BrowsingInstance() { |
| 159 g_browsing_instance_list.Get().erase(this); |
| 106 // We should only be deleted when all of the SiteInstances that refer to | 160 // We should only be deleted when all of the SiteInstances that refer to |
| 107 // us are gone. | 161 // us are gone. |
| 108 DCHECK(site_instance_map_.empty()); | 162 DCHECK(site_instance_map_.empty()); |
| 109 DCHECK_EQ(0u, active_contents_count_); | 163 DCHECK_EQ(0u, active_contents_count_); |
| 110 } | 164 } |
| 111 | 165 |
| 112 } // namespace content | 166 } // namespace content |
| OLD | NEW |