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