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