Chromium Code Reviews| 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/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_controller_factory.h" | |
| 13 #include "content/public/common/content_switches.h" | 12 #include "content/public/common/content_switches.h" |
| 14 #include "content/public/common/url_constants.h" | 13 #include "content/public/common/url_constants.h" |
| 15 | 14 |
| 16 using content::SiteInstance; | 15 using content::SiteInstance; |
| 17 using content::WebUIControllerFactory; | |
| 18 | |
| 19 // static | |
| 20 base::LazyInstance<BrowsingInstance::ContextSiteInstanceMap>::Leaky | |
| 21 BrowsingInstance::context_site_instance_map_ = LAZY_INSTANCE_INITIALIZER; | |
| 22 | 16 |
| 23 BrowsingInstance::BrowsingInstance(content::BrowserContext* browser_context) | 17 BrowsingInstance::BrowsingInstance(content::BrowserContext* browser_context) |
| 24 : browser_context_(browser_context) { | 18 : browser_context_(browser_context) { |
| 25 } | 19 } |
| 26 | 20 |
| 27 bool BrowsingInstance::ShouldUseProcessPerSite(const GURL& url) { | |
| 28 // Returns true if we should use the process-per-site model. This will be | |
| 29 // the case if the --process-per-site switch is specified, or in | |
| 30 // process-per-site-instance for particular sites (e.g., the new tab page). | |
| 31 | |
| 32 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
| 33 if (command_line.HasSwitch(switches::kProcessPerSite)) | |
| 34 return true; | |
| 35 | |
| 36 // We want to consolidate particular sites like extensions and WebUI whether | |
| 37 // it is in process-per-tab or process-per-site-instance. | |
| 38 // Note that --single-process may have been specified, but that affects the | |
| 39 // process creation logic in RenderProcessHost, so we do not need to worry | |
| 40 // about it here. | |
| 41 | |
| 42 if (content::GetContentClient()->browser()-> | |
| 43 ShouldUseProcessPerSite(browser_context_, url)) | |
| 44 return true; | |
| 45 | |
| 46 // DevTools pages have WebUI type but should not reuse the same host. | |
| 47 WebUIControllerFactory* factory = | |
| 48 content::GetContentClient()->browser()->GetWebUIControllerFactory(); | |
| 49 if (factory && factory->UseWebUIForURL(browser_context_, url) && | |
| 50 !url.SchemeIs(chrome::kChromeDevToolsScheme)) { | |
| 51 return true; | |
| 52 } | |
| 53 | |
| 54 // In all other cases, don't use process-per-site logic. | |
| 55 return false; | |
| 56 } | |
| 57 | |
| 58 BrowsingInstance::SiteInstanceMap* BrowsingInstance::GetSiteInstanceMap( | |
| 59 content::BrowserContext* browser_context, const GURL& url) { | |
| 60 if (!ShouldUseProcessPerSite( | |
| 61 SiteInstanceImpl::GetEffectiveURL(browser_context, url))) { | |
| 62 // Not using process-per-site, so use a map specific to this instance. | |
| 63 return &site_instance_map_; | |
| 64 } | |
| 65 | |
| 66 // Otherwise, process-per-site is in use, at least for this URL. Look up the | |
| 67 // global map for this context, creating an entry if necessary. | |
| 68 return &context_site_instance_map_.Get()[browser_context]; | |
| 69 } | |
| 70 | |
| 71 bool BrowsingInstance::HasSiteInstance(const GURL& url) { | 21 bool BrowsingInstance::HasSiteInstance(const GURL& url) { |
| 72 std::string site = | 22 std::string site = |
| 73 SiteInstanceImpl::GetSiteForURL(browser_context_, url) | 23 SiteInstanceImpl::GetSiteForURL(browser_context_, url) |
| 74 .possibly_invalid_spec(); | 24 .possibly_invalid_spec(); |
| 75 | 25 |
| 76 SiteInstanceMap* map = GetSiteInstanceMap(browser_context_, url); | 26 SiteInstanceMap::iterator i = site_instance_map_.find(site); |
|
awong
2012/06/27 00:26:54
How about just doing
return site_instance_map_.fi
Charlie Reis
2012/06/27 20:53:43
Done.
| |
| 77 SiteInstanceMap::iterator i = map->find(site); | 27 return (i != site_instance_map_.end()); |
| 78 return (i != map->end()); | |
| 79 } | 28 } |
| 80 | 29 |
| 81 SiteInstance* BrowsingInstance::GetSiteInstanceForURL(const GURL& url) { | 30 SiteInstance* BrowsingInstance::GetSiteInstanceForURL(const GURL& url) { |
| 82 std::string site = | 31 std::string site = |
| 83 SiteInstanceImpl::GetSiteForURL(browser_context_, url) | 32 SiteInstanceImpl::GetSiteForURL(browser_context_, url) |
| 84 .possibly_invalid_spec(); | 33 .possibly_invalid_spec(); |
| 85 | 34 |
| 86 SiteInstanceMap* map = GetSiteInstanceMap(browser_context_, url); | 35 SiteInstanceMap::iterator i = site_instance_map_.find(site); |
| 87 SiteInstanceMap::iterator i = map->find(site); | 36 if (i != site_instance_map_.end()) { |
| 88 if (i != map->end()) { | |
| 89 return i->second; | 37 return i->second; |
| 90 } | 38 } |
| 91 | 39 |
| 92 // No current SiteInstance for this site, so let's create one. | 40 // No current SiteInstance for this site, so let's create one. |
| 93 SiteInstanceImpl* instance = new SiteInstanceImpl(this); | 41 SiteInstanceImpl* instance = new SiteInstanceImpl(this); |
| 94 | 42 |
| 95 // Set the site of this new SiteInstance, which will register it with us. | 43 // Set the site of this new SiteInstance, which will register it with us. |
| 96 instance->SetSite(url); | 44 instance->SetSite(url); |
| 97 return instance; | 45 return instance; |
| 98 } | 46 } |
| 99 | 47 |
| 100 void BrowsingInstance::RegisterSiteInstance(SiteInstance* site_instance) { | 48 void BrowsingInstance::RegisterSiteInstance(SiteInstance* site_instance) { |
| 101 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)-> | 49 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)-> |
| 102 browsing_instance_ == this); | 50 browsing_instance_ == this); |
| 103 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)->HasSite()); | 51 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)->HasSite()); |
| 104 std::string site = site_instance->GetSite().possibly_invalid_spec(); | 52 std::string site = site_instance->GetSite().possibly_invalid_spec(); |
| 105 | 53 |
| 106 // Only register if we don't have a SiteInstance for this site already. | 54 // Only register if we don't have a SiteInstance for this site already. |
| 107 // It's possible to have two SiteInstances point to the same site if two | 55 // It's possible to have two SiteInstances point to the same site if two |
| 108 // tabs are navigated there at the same time. (We don't call SetSite or | 56 // tabs are navigated there at the same time. (We don't call SetSite or |
| 109 // register them until DidNavigate.) If there is a previously existing | 57 // register them until DidNavigate.) If there is a previously existing |
| 110 // SiteInstance for this site, we just won't register the new one. | 58 // SiteInstance for this site, we just won't register the new one. |
| 111 SiteInstanceMap* map = GetSiteInstanceMap(browser_context_, | 59 SiteInstanceMap::iterator i = site_instance_map_.find(site); |
| 112 site_instance->GetSite()); | 60 if (i == site_instance_map_.end()) { |
| 113 SiteInstanceMap::iterator i = map->find(site); | |
| 114 if (i == map->end()) { | |
| 115 // Not previously registered, so register it. | 61 // Not previously registered, so register it. |
| 116 (*map)[site] = site_instance; | 62 site_instance_map_[site] = site_instance; |
| 117 } | 63 } |
| 118 } | 64 } |
| 119 | 65 |
| 120 void BrowsingInstance::UnregisterSiteInstance(SiteInstance* site_instance) { | 66 void BrowsingInstance::UnregisterSiteInstance(SiteInstance* site_instance) { |
| 121 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)-> | 67 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)-> |
| 122 browsing_instance_ == this); | 68 browsing_instance_ == this); |
| 123 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)->HasSite()); | 69 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)->HasSite()); |
| 124 std::string site = site_instance->GetSite().possibly_invalid_spec(); | 70 std::string site = site_instance->GetSite().possibly_invalid_spec(); |
| 125 | 71 |
| 126 // Only unregister the SiteInstance if it is the same one that is registered | 72 // Only unregister the SiteInstance if it is the same one that is registered |
| 127 // for the site. (It might have been an unregistered SiteInstance. See the | 73 // for the site. (It might have been an unregistered SiteInstance. See the |
| 128 // comments in RegisterSiteInstance.) | 74 // comments in RegisterSiteInstance.) |
| 129 | 75 SiteInstanceMap::iterator i = site_instance_map_.find(site); |
| 130 // We look for the site instance in both the local site_instance_map_ and also | 76 if (i != site_instance_map_.end() && i->second == site_instance) { |
| 131 // the static context_site_instance_map_ - this is because the logic in | 77 // Matches, so erase it. |
| 132 // ShouldUseProcessPerSite() can produce different results over the lifetime | 78 site_instance_map_.erase(i); |
| 133 // of Chrome (e.g. installation of apps with web extents can change our | |
| 134 // process-per-site policy for a given domain), so we don't know which map | |
| 135 // the site was put into when it was originally registered. | |
| 136 if (!RemoveSiteInstanceFromMap(&site_instance_map_, site, site_instance)) { | |
| 137 // Wasn't in our local map, so look in the static per-browser context map. | |
| 138 RemoveSiteInstanceFromMap( | |
| 139 &context_site_instance_map_.Get()[browser_context_], | |
| 140 site, | |
| 141 site_instance); | |
| 142 } | 79 } |
| 143 } | 80 } |
| 144 | 81 |
| 145 bool BrowsingInstance::RemoveSiteInstanceFromMap( | |
| 146 SiteInstanceMap* map, | |
| 147 const std::string& site, | |
| 148 SiteInstance* site_instance) { | |
| 149 SiteInstanceMap::iterator i = map->find(site); | |
| 150 if (i != map->end() && i->second == site_instance) { | |
| 151 // Matches, so erase it. | |
| 152 map->erase(i); | |
| 153 return true; | |
| 154 } | |
| 155 return false; | |
| 156 } | |
| 157 | |
| 158 BrowsingInstance::~BrowsingInstance() { | 82 BrowsingInstance::~BrowsingInstance() { |
| 159 // We should only be deleted when all of the SiteInstances that refer to | 83 // We should only be deleted when all of the SiteInstances that refer to |
| 160 // us are gone. | 84 // us are gone. |
| 161 DCHECK(site_instance_map_.empty()); | 85 DCHECK(site_instance_map_.empty()); |
| 162 } | 86 } |
| OLD | NEW |