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* context) |
22 : profile_(profile) { | 22 : context_(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()->ShouldUseProcessPerSite(context_, |
41 url)) | 41 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(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* context, const GURL& url) { |
56 if (!ShouldUseProcessPerSite(SiteInstance::GetEffectiveURL(profile, url))) { | 56 if (!ShouldUseProcessPerSite(SiteInstance::GetEffectiveURL(context, url))) { |
57 // Not using process-per-site, so use a map specific to this instance. | 57 // Not using process-per-site, so use a map specific to this instance. |
58 return &site_instance_map_; | 58 return &site_instance_map_; |
59 } | 59 } |
60 | 60 |
61 // Otherwise, process-per-site is in use, at least for this URL. Look up the | 61 // 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. | 62 // global map for this context, creating an entry if necessary. |
63 return &profile_site_instance_map_[profile]; | 63 return &context_site_instance_map_[context]; |
64 } | 64 } |
65 | 65 |
66 bool BrowsingInstance::HasSiteInstance(const GURL& url) { | 66 bool BrowsingInstance::HasSiteInstance(const GURL& url) { |
67 std::string site = | 67 std::string site = |
68 SiteInstance::GetSiteForURL(profile_, url).possibly_invalid_spec(); | 68 SiteInstance::GetSiteForURL(context_, url).possibly_invalid_spec(); |
69 | 69 |
70 SiteInstanceMap* map = GetSiteInstanceMap(profile_, url); | 70 SiteInstanceMap* map = GetSiteInstanceMap(context_, url); |
71 SiteInstanceMap::iterator i = map->find(site); | 71 SiteInstanceMap::iterator i = map->find(site); |
72 return (i != map->end()); | 72 return (i != map->end()); |
73 } | 73 } |
74 | 74 |
75 SiteInstance* BrowsingInstance::GetSiteInstanceForURL(const GURL& url) { | 75 SiteInstance* BrowsingInstance::GetSiteInstanceForURL(const GURL& url) { |
76 std::string site = | 76 std::string site = |
77 SiteInstance::GetSiteForURL(profile_, url).possibly_invalid_spec(); | 77 SiteInstance::GetSiteForURL(context_, url).possibly_invalid_spec(); |
78 | 78 |
79 SiteInstanceMap* map = GetSiteInstanceMap(profile_, url); | 79 SiteInstanceMap* map = GetSiteInstanceMap(context_, url); |
80 SiteInstanceMap::iterator i = map->find(site); | 80 SiteInstanceMap::iterator i = map->find(site); |
81 if (i != map->end()) { | 81 if (i != map->end()) { |
82 return i->second; | 82 return i->second; |
83 } | 83 } |
84 | 84 |
85 // No current SiteInstance for this site, so let's create one. | 85 // No current SiteInstance for this site, so let's create one. |
86 SiteInstance* instance = new SiteInstance(this); | 86 SiteInstance* instance = new SiteInstance(this); |
87 | 87 |
88 // Set the site of this new SiteInstance, which will register it with us. | 88 // Set the site of this new SiteInstance, which will register it with us. |
89 instance->SetSite(url); | 89 instance->SetSite(url); |
90 return instance; | 90 return instance; |
91 } | 91 } |
92 | 92 |
93 void BrowsingInstance::RegisterSiteInstance(SiteInstance* site_instance) { | 93 void BrowsingInstance::RegisterSiteInstance(SiteInstance* site_instance) { |
94 DCHECK(site_instance->browsing_instance() == this); | 94 DCHECK(site_instance->browsing_instance() == this); |
95 DCHECK(site_instance->has_site()); | 95 DCHECK(site_instance->has_site()); |
96 std::string site = site_instance->site().possibly_invalid_spec(); | 96 std::string site = site_instance->site().possibly_invalid_spec(); |
97 | 97 |
98 // Only register if we don't have a SiteInstance for this site already. | 98 // 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 | 99 // 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 | 100 // 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 | 101 // register them until DidNavigate.) If there is a previously existing |
102 // SiteInstance for this site, we just won't register the new one. | 102 // SiteInstance for this site, we just won't register the new one. |
103 SiteInstanceMap* map = GetSiteInstanceMap(profile_, site_instance->site()); | 103 SiteInstanceMap* map = GetSiteInstanceMap(context_, site_instance->site()); |
104 SiteInstanceMap::iterator i = map->find(site); | 104 SiteInstanceMap::iterator i = map->find(site); |
105 if (i == map->end()) { | 105 if (i == map->end()) { |
106 // Not previously registered, so register it. | 106 // Not previously registered, so register it. |
107 (*map)[site] = site_instance; | 107 (*map)[site] = site_instance; |
108 } | 108 } |
109 } | 109 } |
110 | 110 |
111 void BrowsingInstance::UnregisterSiteInstance(SiteInstance* site_instance) { | 111 void BrowsingInstance::UnregisterSiteInstance(SiteInstance* site_instance) { |
112 DCHECK(site_instance->browsing_instance() == this); | 112 DCHECK(site_instance->browsing_instance() == this); |
113 DCHECK(site_instance->has_site()); | 113 DCHECK(site_instance->has_site()); |
114 std::string site = site_instance->site().possibly_invalid_spec(); | 114 std::string site = site_instance->site().possibly_invalid_spec(); |
115 | 115 |
116 // Only unregister the SiteInstance if it is the same one that is registered | 116 // 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 | 117 // for the site. (It might have been an unregistered SiteInstance. See the |
118 // comments in RegisterSiteInstance.) | 118 // comments in RegisterSiteInstance.) |
119 | 119 |
120 // We look for the site instance in both the local site_instance_map_ and also | 120 // 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 | 121 // the static context_site_instance_map_ - this is because the logic in |
122 // ShouldUseProcessPerSite() can produce different results over the lifetime | 122 // ShouldUseProcessPerSite() can produce different results over the lifetime |
123 // of Chrome (e.g. installation of apps with web extents can change our | 123 // 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 | 124 // 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. | 125 // the site was put into when it was originally registered. |
126 if (!RemoveSiteInstanceFromMap(&site_instance_map_, site, site_instance)) { | 126 if (!RemoveSiteInstanceFromMap(&site_instance_map_, site, site_instance)) { |
127 // Wasn't in our local map, so look in the static per-profile map. | 127 // Wasn't in our local map, so look in the static per-context map. |
128 RemoveSiteInstanceFromMap( | 128 RemoveSiteInstanceFromMap( |
129 &profile_site_instance_map_[profile_], site, site_instance); | 129 &context_site_instance_map_[context_], site, site_instance); |
130 } | 130 } |
131 } | 131 } |
132 | 132 |
133 bool BrowsingInstance::RemoveSiteInstanceFromMap(SiteInstanceMap* map, | 133 bool BrowsingInstance::RemoveSiteInstanceFromMap(SiteInstanceMap* map, |
134 const std::string& site, | 134 const std::string& site, |
135 SiteInstance* site_instance) { | 135 SiteInstance* site_instance) { |
136 SiteInstanceMap::iterator i = map->find(site); | 136 SiteInstanceMap::iterator i = map->find(site); |
137 if (i != map->end() && i->second == site_instance) { | 137 if (i != map->end() && i->second == site_instance) { |
138 // Matches, so erase it. | 138 // Matches, so erase it. |
139 map->erase(i); | 139 map->erase(i); |
140 return true; | 140 return true; |
141 } | 141 } |
142 return false; | 142 return false; |
143 } | 143 } |
144 | 144 |
145 BrowsingInstance::~BrowsingInstance() { | 145 BrowsingInstance::~BrowsingInstance() { |
146 // We should only be deleted when all of the SiteInstances that refer to | 146 // We should only be deleted when all of the SiteInstances that refer to |
147 // us are gone. | 147 // us are gone. |
148 DCHECK(site_instance_map_.empty()); | 148 DCHECK(site_instance_map_.empty()); |
149 } | 149 } |
OLD | NEW |