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 3 // found in the LICENSE file. 4 5 #include "content/browser/browsing_instance.h" 6 7 #include "base/command_line.h" 8 #include "base/logging.h" 9 #include "content/browser/site_instance_impl.h" 10 #include "content/public/browser/browser_context.h" 11 #include "content/public/browser/content_browser_client.h" 12 #include "content/public/common/content_switches.h" 13 #include "content/public/common/url_constants.h" 14 15 namespace content { 16 BrowsingInstance(BrowserContext * browser_context)17BrowsingInstance::BrowsingInstance(BrowserContext* browser_context) 18 : browser_context_(browser_context), 19 active_contents_count_(0u) { 20 } 21 HasSiteInstance(const GURL & url)22bool BrowsingInstance::HasSiteInstance(const GURL& url) { 23 std::string site = 24 SiteInstanceImpl::GetSiteForURL(browser_context_, url) 25 .possibly_invalid_spec(); 26 27 return site_instance_map_.find(site) != site_instance_map_.end(); 28 } 29 GetSiteInstanceForURL(const GURL & url)30SiteInstance* BrowsingInstance::GetSiteInstanceForURL(const GURL& url) { 31 std::string site = 32 SiteInstanceImpl::GetSiteForURL(browser_context_, url) 33 .possibly_invalid_spec(); 34 35 SiteInstanceMap::iterator i = site_instance_map_.find(site); 36 if (i != site_instance_map_.end()) 37 return i->second; 38 39 40 // No current SiteInstance for this site, so let's create one. 41 SiteInstanceImpl* instance = new SiteInstanceImpl(this); 42 43 // Set the site of this new SiteInstance, which will register it with us. 44 instance->SetSite(url); 45 return instance; 46 } 47 RegisterSiteInstance(SiteInstance * site_instance)48void BrowsingInstance::RegisterSiteInstance(SiteInstance* site_instance) { 49 DCHECK(static_cast<SiteInstanceImpl*>(site_instance) 50 ->browsing_instance_.get() == 51 this); 52 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)->HasSite()); 53 std::string site = site_instance->GetSiteURL().possibly_invalid_spec(); 54 55 // Only register if we don't have a SiteInstance for this site already. 56 // It's possible to have two SiteInstances point to the same site if two 57 // tabs are navigated there at the same time. (We don't call SetSite or 58 // register them until DidNavigate.) If there is a previously existing 59 // SiteInstance for this site, we just won't register the new one. 60 SiteInstanceMap::iterator i = site_instance_map_.find(site); 61 if (i == site_instance_map_.end()) { 62 // Not previously registered, so register it. 63 site_instance_map_[site] = site_instance; 64 } 65 } 66 UnregisterSiteInstance(SiteInstance * site_instance)67void BrowsingInstance::UnregisterSiteInstance(SiteInstance* site_instance) { 68 DCHECK(static_cast<SiteInstanceImpl*>(site_instance) 69 ->browsing_instance_.get() == 70 this); 71 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)->HasSite()); 72 std::string site = site_instance->GetSiteURL().possibly_invalid_spec(); 73 74 // Only unregister the SiteInstance if it is the same one that is registered 75 // for the site. (It might have been an unregistered SiteInstance. See the 76 // comments in RegisterSiteInstance.) 77 SiteInstanceMap::iterator i = site_instance_map_.find(site); 78 if (i != site_instance_map_.end() && i->second == site_instance) { 79 // Matches, so erase it. 80 site_instance_map_.erase(i); 81 } 82 } 83 ~BrowsingInstance()84BrowsingInstance::~BrowsingInstance() { 85 // We should only be deleted when all of the SiteInstances that refer to 86 // us are gone. 87 DCHECK(site_instance_map_.empty()); 88 DCHECK_EQ(0u, active_contents_count_); 89 } 90 91 } // namespace content 92