| 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.h" | 9 #include "content/browser/site_instance_impl.h" |
| 10 #include "content/public/browser/browser_context.h" | 10 #include "content/public/browser/browser_context.h" |
| 11 #include "content/public/browser/content_browser_client.h" | 11 #include "content/public/browser/content_browser_client.h" |
| 12 #include "content/public/browser/web_ui_factory.h" | 12 #include "content/public/browser/web_ui_factory.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 using content::SiteInstance; |
| 17 |
| 16 // static | 18 // static |
| 17 base::LazyInstance< | 19 base::LazyInstance< |
| 18 BrowsingInstance::ContextSiteInstanceMap, | 20 BrowsingInstance::ContextSiteInstanceMap, |
| 19 base::LeakyLazyInstanceTraits<BrowsingInstance::ContextSiteInstanceMap> > | 21 base::LeakyLazyInstanceTraits<BrowsingInstance::ContextSiteInstanceMap> > |
| 20 BrowsingInstance::context_site_instance_map_ = | 22 BrowsingInstance::context_site_instance_map_ = |
| 21 LAZY_INSTANCE_INITIALIZER; | 23 LAZY_INSTANCE_INITIALIZER; |
| 22 | 24 |
| 23 BrowsingInstance::BrowsingInstance(content::BrowserContext* browser_context) | 25 BrowsingInstance::BrowsingInstance(content::BrowserContext* browser_context) |
| 24 : browser_context_(browser_context) { | 26 : browser_context_(browser_context) { |
| 25 } | 27 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 49 !url.SchemeIs(chrome::kChromeDevToolsScheme)) { | 51 !url.SchemeIs(chrome::kChromeDevToolsScheme)) { |
| 50 return true; | 52 return true; |
| 51 } | 53 } |
| 52 | 54 |
| 53 // In all other cases, don't use process-per-site logic. | 55 // In all other cases, don't use process-per-site logic. |
| 54 return false; | 56 return false; |
| 55 } | 57 } |
| 56 | 58 |
| 57 BrowsingInstance::SiteInstanceMap* BrowsingInstance::GetSiteInstanceMap( | 59 BrowsingInstance::SiteInstanceMap* BrowsingInstance::GetSiteInstanceMap( |
| 58 content::BrowserContext* browser_context, const GURL& url) { | 60 content::BrowserContext* browser_context, const GURL& url) { |
| 59 if (!ShouldUseProcessPerSite(SiteInstance::GetEffectiveURL(browser_context, | 61 if (!ShouldUseProcessPerSite( |
| 60 url))) { | 62 SiteInstanceImpl::GetEffectiveURL(browser_context, url))) { |
| 61 // Not using process-per-site, so use a map specific to this instance. | 63 // Not using process-per-site, so use a map specific to this instance. |
| 62 return &site_instance_map_; | 64 return &site_instance_map_; |
| 63 } | 65 } |
| 64 | 66 |
| 65 // Otherwise, process-per-site is in use, at least for this URL. Look up the | 67 // Otherwise, process-per-site is in use, at least for this URL. Look up the |
| 66 // global map for this context, creating an entry if necessary. | 68 // global map for this context, creating an entry if necessary. |
| 67 return &context_site_instance_map_.Get()[browser_context]; | 69 return &context_site_instance_map_.Get()[browser_context]; |
| 68 } | 70 } |
| 69 | 71 |
| 70 bool BrowsingInstance::HasSiteInstance(const GURL& url) { | 72 bool BrowsingInstance::HasSiteInstance(const GURL& url) { |
| 71 std::string site = | 73 std::string site = |
| 72 SiteInstance::GetSiteForURL(browser_context_, url) | 74 SiteInstanceImpl::GetSiteForURL(browser_context_, url) |
| 73 .possibly_invalid_spec(); | 75 .possibly_invalid_spec(); |
| 74 | 76 |
| 75 SiteInstanceMap* map = GetSiteInstanceMap(browser_context_, url); | 77 SiteInstanceMap* map = GetSiteInstanceMap(browser_context_, url); |
| 76 SiteInstanceMap::iterator i = map->find(site); | 78 SiteInstanceMap::iterator i = map->find(site); |
| 77 return (i != map->end()); | 79 return (i != map->end()); |
| 78 } | 80 } |
| 79 | 81 |
| 80 SiteInstance* BrowsingInstance::GetSiteInstanceForURL(const GURL& url) { | 82 SiteInstance* BrowsingInstance::GetSiteInstanceForURL(const GURL& url) { |
| 81 std::string site = | 83 std::string site = |
| 82 SiteInstance::GetSiteForURL(browser_context_, url) | 84 SiteInstanceImpl::GetSiteForURL(browser_context_, url) |
| 83 .possibly_invalid_spec(); | 85 .possibly_invalid_spec(); |
| 84 | 86 |
| 85 SiteInstanceMap* map = GetSiteInstanceMap(browser_context_, url); | 87 SiteInstanceMap* map = GetSiteInstanceMap(browser_context_, url); |
| 86 SiteInstanceMap::iterator i = map->find(site); | 88 SiteInstanceMap::iterator i = map->find(site); |
| 87 if (i != map->end()) { | 89 if (i != map->end()) { |
| 88 return i->second; | 90 return i->second; |
| 89 } | 91 } |
| 90 | 92 |
| 91 // No current SiteInstance for this site, so let's create one. | 93 // No current SiteInstance for this site, so let's create one. |
| 92 SiteInstance* instance = new SiteInstance(this); | 94 SiteInstanceImpl* instance = new SiteInstanceImpl(this); |
| 93 | 95 |
| 94 // Set the site of this new SiteInstance, which will register it with us. | 96 // Set the site of this new SiteInstance, which will register it with us. |
| 95 instance->SetSite(url); | 97 instance->SetSite(url); |
| 96 return instance; | 98 return instance; |
| 97 } | 99 } |
| 98 | 100 |
| 99 void BrowsingInstance::RegisterSiteInstance(SiteInstance* site_instance) { | 101 void BrowsingInstance::RegisterSiteInstance(SiteInstance* site_instance) { |
| 100 DCHECK(site_instance->browsing_instance_ == this); | 102 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)-> |
| 101 DCHECK(site_instance->has_site()); | 103 browsing_instance_ == this); |
| 102 std::string site = site_instance->site().possibly_invalid_spec(); | 104 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)->HasSite()); |
| 105 std::string site = site_instance->GetSite().possibly_invalid_spec(); |
| 103 | 106 |
| 104 // Only register if we don't have a SiteInstance for this site already. | 107 // Only register if we don't have a SiteInstance for this site already. |
| 105 // It's possible to have two SiteInstances point to the same site if two | 108 // It's possible to have two SiteInstances point to the same site if two |
| 106 // tabs are navigated there at the same time. (We don't call SetSite or | 109 // tabs are navigated there at the same time. (We don't call SetSite or |
| 107 // register them until DidNavigate.) If there is a previously existing | 110 // register them until DidNavigate.) If there is a previously existing |
| 108 // SiteInstance for this site, we just won't register the new one. | 111 // SiteInstance for this site, we just won't register the new one. |
| 109 SiteInstanceMap* map = GetSiteInstanceMap(browser_context_, | 112 SiteInstanceMap* map = GetSiteInstanceMap(browser_context_, |
| 110 site_instance->site()); | 113 site_instance->GetSite()); |
| 111 SiteInstanceMap::iterator i = map->find(site); | 114 SiteInstanceMap::iterator i = map->find(site); |
| 112 if (i == map->end()) { | 115 if (i == map->end()) { |
| 113 // Not previously registered, so register it. | 116 // Not previously registered, so register it. |
| 114 (*map)[site] = site_instance; | 117 (*map)[site] = site_instance; |
| 115 } | 118 } |
| 116 } | 119 } |
| 117 | 120 |
| 118 void BrowsingInstance::UnregisterSiteInstance(SiteInstance* site_instance) { | 121 void BrowsingInstance::UnregisterSiteInstance(SiteInstance* site_instance) { |
| 119 DCHECK(site_instance->browsing_instance_ == this); | 122 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)-> |
| 120 DCHECK(site_instance->has_site()); | 123 browsing_instance_ == this); |
| 121 std::string site = site_instance->site().possibly_invalid_spec(); | 124 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)->HasSite()); |
| 125 std::string site = site_instance->GetSite().possibly_invalid_spec(); |
| 122 | 126 |
| 123 // Only unregister the SiteInstance if it is the same one that is registered | 127 // Only unregister the SiteInstance if it is the same one that is registered |
| 124 // for the site. (It might have been an unregistered SiteInstance. See the | 128 // for the site. (It might have been an unregistered SiteInstance. See the |
| 125 // comments in RegisterSiteInstance.) | 129 // comments in RegisterSiteInstance.) |
| 126 | 130 |
| 127 // We look for the site instance in both the local site_instance_map_ and also | 131 // We look for the site instance in both the local site_instance_map_ and also |
| 128 // the static context_site_instance_map_ - this is because the logic in | 132 // the static context_site_instance_map_ - this is because the logic in |
| 129 // ShouldUseProcessPerSite() can produce different results over the lifetime | 133 // ShouldUseProcessPerSite() can produce different results over the lifetime |
| 130 // of Chrome (e.g. installation of apps with web extents can change our | 134 // of Chrome (e.g. installation of apps with web extents can change our |
| 131 // process-per-site policy for a given domain), so we don't know which map | 135 // process-per-site policy for a given domain), so we don't know which map |
| 132 // the site was put into when it was originally registered. | 136 // the site was put into when it was originally registered. |
| 133 if (!RemoveSiteInstanceFromMap(&site_instance_map_, site, site_instance)) { | 137 if (!RemoveSiteInstanceFromMap(&site_instance_map_, site, site_instance)) { |
| 134 // Wasn't in our local map, so look in the static per-browser context map. | 138 // Wasn't in our local map, so look in the static per-browser context map. |
| 135 RemoveSiteInstanceFromMap( | 139 RemoveSiteInstanceFromMap( |
| 136 &context_site_instance_map_.Get()[browser_context_], | 140 &context_site_instance_map_.Get()[browser_context_], |
| 137 site, | 141 site, |
| 138 site_instance); | 142 site_instance); |
| 139 } | 143 } |
| 140 } | 144 } |
| 141 | 145 |
| 142 bool BrowsingInstance::RemoveSiteInstanceFromMap(SiteInstanceMap* map, | 146 bool BrowsingInstance::RemoveSiteInstanceFromMap( |
| 143 const std::string& site, | 147 SiteInstanceMap* map, |
| 144 SiteInstance* site_instance) { | 148 const std::string& site, |
| 149 SiteInstance* site_instance) { |
| 145 SiteInstanceMap::iterator i = map->find(site); | 150 SiteInstanceMap::iterator i = map->find(site); |
| 146 if (i != map->end() && i->second == site_instance) { | 151 if (i != map->end() && i->second == site_instance) { |
| 147 // Matches, so erase it. | 152 // Matches, so erase it. |
| 148 map->erase(i); | 153 map->erase(i); |
| 149 return true; | 154 return true; |
| 150 } | 155 } |
| 151 return false; | 156 return false; |
| 152 } | 157 } |
| 153 | 158 |
| 154 BrowsingInstance::~BrowsingInstance() { | 159 BrowsingInstance::~BrowsingInstance() { |
| 155 // 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 |
| 156 // us are gone. | 161 // us are gone. |
| 157 DCHECK(site_instance_map_.empty()); | 162 DCHECK(site_instance_map_.empty()); |
| 158 } | 163 } |
| OLD | NEW |