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 return site_instance_map_.find(site) != site_instance_map_.end(); |
77 SiteInstanceMap::iterator i = map->find(site); | |
78 return (i != map->end()); | |
79 } | 27 } |
80 | 28 |
81 SiteInstance* BrowsingInstance::GetSiteInstanceForURL(const GURL& url) { | 29 SiteInstance* BrowsingInstance::GetSiteInstanceForURL(const GURL& url) { |
82 std::string site = | 30 std::string site = |
83 SiteInstanceImpl::GetSiteForURL(browser_context_, url) | 31 SiteInstanceImpl::GetSiteForURL(browser_context_, url) |
84 .possibly_invalid_spec(); | 32 .possibly_invalid_spec(); |
85 | 33 |
86 SiteInstanceMap* map = GetSiteInstanceMap(browser_context_, url); | 34 SiteInstanceMap::iterator i = site_instance_map_.find(site); |
87 SiteInstanceMap::iterator i = map->find(site); | 35 if (i != site_instance_map_.end()) |
88 if (i != map->end()) { | |
89 return i->second; | 36 return i->second; |
90 } | 37 |
91 | 38 |
92 // No current SiteInstance for this site, so let's create one. | 39 // No current SiteInstance for this site, so let's create one. |
93 SiteInstanceImpl* instance = new SiteInstanceImpl(this); | 40 SiteInstanceImpl* instance = new SiteInstanceImpl(this); |
94 | 41 |
95 // Set the site of this new SiteInstance, which will register it with us. | 42 // Set the site of this new SiteInstance, which will register it with us. |
96 instance->SetSite(url); | 43 instance->SetSite(url); |
97 return instance; | 44 return instance; |
98 } | 45 } |
99 | 46 |
100 void BrowsingInstance::RegisterSiteInstance(SiteInstance* site_instance) { | 47 void BrowsingInstance::RegisterSiteInstance(SiteInstance* site_instance) { |
101 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)-> | 48 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)-> |
102 browsing_instance_ == this); | 49 browsing_instance_ == this); |
103 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)->HasSite()); | 50 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)->HasSite()); |
104 std::string site = site_instance->GetSite().possibly_invalid_spec(); | 51 std::string site = site_instance->GetSite().possibly_invalid_spec(); |
105 | 52 |
106 // Only register if we don't have a SiteInstance for this site already. | 53 // 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 | 54 // 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 | 55 // 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 | 56 // register them until DidNavigate.) If there is a previously existing |
110 // SiteInstance for this site, we just won't register the new one. | 57 // SiteInstance for this site, we just won't register the new one. |
111 SiteInstanceMap* map = GetSiteInstanceMap(browser_context_, | 58 SiteInstanceMap::iterator i = site_instance_map_.find(site); |
112 site_instance->GetSite()); | 59 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. | 60 // Not previously registered, so register it. |
116 (*map)[site] = site_instance; | 61 site_instance_map_[site] = site_instance; |
117 } | 62 } |
118 } | 63 } |
119 | 64 |
120 void BrowsingInstance::UnregisterSiteInstance(SiteInstance* site_instance) { | 65 void BrowsingInstance::UnregisterSiteInstance(SiteInstance* site_instance) { |
121 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)-> | 66 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)-> |
122 browsing_instance_ == this); | 67 browsing_instance_ == this); |
123 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)->HasSite()); | 68 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)->HasSite()); |
124 std::string site = site_instance->GetSite().possibly_invalid_spec(); | 69 std::string site = site_instance->GetSite().possibly_invalid_spec(); |
125 | 70 |
126 // Only unregister the SiteInstance if it is the same one that is registered | 71 // 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 | 72 // for the site. (It might have been an unregistered SiteInstance. See the |
128 // comments in RegisterSiteInstance.) | 73 // comments in RegisterSiteInstance.) |
129 | 74 SiteInstanceMap::iterator i = site_instance_map_.find(site); |
130 // We look for the site instance in both the local site_instance_map_ and also | 75 if (i != site_instance_map_.end() && i->second == site_instance) { |
131 // the static context_site_instance_map_ - this is because the logic in | 76 // Matches, so erase it. |
132 // ShouldUseProcessPerSite() can produce different results over the lifetime | 77 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 } | 78 } |
143 } | 79 } |
144 | 80 |
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() { | 81 BrowsingInstance::~BrowsingInstance() { |
159 // We should only be deleted when all of the SiteInstances that refer to | 82 // We should only be deleted when all of the SiteInstances that refer to |
160 // us are gone. | 83 // us are gone. |
161 DCHECK(site_instance_map_.empty()); | 84 DCHECK(site_instance_map_.empty()); |
162 } | 85 } |
OLD | NEW |