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