OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/renderer_host/site_instance.h" | |
6 | |
7 #include "chrome/browser/browsing_instance.h" | |
8 #include "chrome/browser/extensions/extension_service.h" | |
9 #include "chrome/browser/renderer_host/browser_render_process_host.h" | |
10 #include "chrome/browser/webui/web_ui_factory.h" | |
11 #include "chrome/common/notification_service.h" | |
12 #include "chrome/common/url_constants.h" | |
13 #include "net/base/registry_controlled_domain.h" | |
14 | |
15 // We treat javascript:, about:crash, about:hang, and about:shorthang as the | |
16 // same site as any URL since they are actually modifiers on existing pages. | |
17 static bool IsURLSameAsAnySiteInstance(const GURL& url) { | |
18 if (!url.is_valid()) | |
19 return false; | |
20 return url.SchemeIs(chrome::kJavaScriptScheme) || | |
21 url.spec() == chrome::kAboutCrashURL || | |
22 url.spec() == chrome::kAboutKillURL || | |
23 url.spec() == chrome::kAboutHangURL || | |
24 url.spec() == chrome::kAboutShorthangURL; | |
25 } | |
26 | |
27 SiteInstance::SiteInstance(BrowsingInstance* browsing_instance) | |
28 : browsing_instance_(browsing_instance), | |
29 render_process_host_factory_(NULL), | |
30 process_(NULL), | |
31 max_page_id_(-1), | |
32 has_site_(false) { | |
33 DCHECK(browsing_instance); | |
34 | |
35 registrar_.Add(this, NotificationType::RENDERER_PROCESS_TERMINATED, | |
36 NotificationService::AllSources()); | |
37 } | |
38 | |
39 SiteInstance::~SiteInstance() { | |
40 // Now that no one is referencing us, we can safely remove ourselves from | |
41 // the BrowsingInstance. Any future visits to a page from this site | |
42 // (within the same BrowsingInstance) can safely create a new SiteInstance. | |
43 if (has_site_) | |
44 browsing_instance_->UnregisterSiteInstance(this); | |
45 } | |
46 | |
47 bool SiteInstance::HasProcess() const { | |
48 return (process_ != NULL); | |
49 } | |
50 | |
51 RenderProcessHost* SiteInstance::GetProcess() { | |
52 // TODO(erikkay) It would be nice to ensure that the renderer type had been | |
53 // properly set before we get here. The default tab creation case winds up | |
54 // with no site set at this point, so it will default to TYPE_NORMAL. This | |
55 // may not be correct, so we'll wind up potentially creating a process that | |
56 // we then throw away, or worse sharing a process with the wrong process type. | |
57 // See crbug.com/43448. | |
58 | |
59 // Create a new process if ours went away or was reused. | |
60 if (!process_) { | |
61 // See if we should reuse an old process | |
62 if (RenderProcessHost::ShouldTryToUseExistingProcessHost()) | |
63 process_ = RenderProcessHost::GetExistingProcessHost( | |
64 browsing_instance_->profile(), GetRendererType()); | |
65 | |
66 // Otherwise (or if that fails), create a new one. | |
67 if (!process_) { | |
68 if (render_process_host_factory_) { | |
69 process_ = render_process_host_factory_->CreateRenderProcessHost( | |
70 browsing_instance_->profile()); | |
71 } else { | |
72 process_ = new BrowserRenderProcessHost(browsing_instance_->profile()); | |
73 } | |
74 } | |
75 | |
76 // Make sure the process starts at the right max_page_id | |
77 process_->UpdateMaxPageID(max_page_id_); | |
78 } | |
79 DCHECK(process_); | |
80 | |
81 return process_; | |
82 } | |
83 | |
84 void SiteInstance::SetSite(const GURL& url) { | |
85 // A SiteInstance's site should not change. | |
86 // TODO(creis): When following links or script navigations, we can currently | |
87 // render pages from other sites in this SiteInstance. This will eventually | |
88 // be fixed, but until then, we should still not set the site of a | |
89 // SiteInstance more than once. | |
90 DCHECK(!has_site_); | |
91 | |
92 // Remember that this SiteInstance has been used to load a URL, even if the | |
93 // URL is invalid. | |
94 has_site_ = true; | |
95 site_ = GetSiteForURL(browsing_instance_->profile(), url); | |
96 | |
97 // Now that we have a site, register it with the BrowsingInstance. This | |
98 // ensures that we won't create another SiteInstance for this site within | |
99 // the same BrowsingInstance, because all same-site pages within a | |
100 // BrowsingInstance can script each other. | |
101 browsing_instance_->RegisterSiteInstance(this); | |
102 } | |
103 | |
104 bool SiteInstance::HasRelatedSiteInstance(const GURL& url) { | |
105 return browsing_instance_->HasSiteInstance(url); | |
106 } | |
107 | |
108 SiteInstance* SiteInstance::GetRelatedSiteInstance(const GURL& url) { | |
109 return browsing_instance_->GetSiteInstanceForURL(url); | |
110 } | |
111 | |
112 /*static*/ | |
113 SiteInstance* SiteInstance::CreateSiteInstance(Profile* profile) { | |
114 return new SiteInstance(new BrowsingInstance(profile)); | |
115 } | |
116 | |
117 /*static*/ | |
118 SiteInstance* SiteInstance::CreateSiteInstanceForURL(Profile* profile, | |
119 const GURL& url) { | |
120 // This BrowsingInstance may be deleted if it returns an existing | |
121 // SiteInstance. | |
122 scoped_refptr<BrowsingInstance> instance(new BrowsingInstance(profile)); | |
123 return instance->GetSiteInstanceForURL(url); | |
124 } | |
125 | |
126 /*static*/ | |
127 GURL SiteInstance::GetSiteForURL(Profile* profile, const GURL& real_url) { | |
128 GURL url = GetEffectiveURL(profile, real_url); | |
129 | |
130 // URLs with no host should have an empty site. | |
131 GURL site; | |
132 | |
133 // TODO(creis): For many protocols, we should just treat the scheme as the | |
134 // site, since there is no host. e.g., file:, about:, chrome: | |
135 | |
136 // If the url has a host, then determine the site. | |
137 if (url.has_host()) { | |
138 // Only keep the scheme and registered domain as given by GetOrigin. This | |
139 // may also include a port, which we need to drop. | |
140 site = url.GetOrigin(); | |
141 | |
142 // Remove port, if any. | |
143 if (site.has_port()) { | |
144 GURL::Replacements rep; | |
145 rep.ClearPort(); | |
146 site = site.ReplaceComponents(rep); | |
147 } | |
148 | |
149 // If this URL has a registered domain, we only want to remember that part. | |
150 std::string domain = | |
151 net::RegistryControlledDomainService::GetDomainAndRegistry(url); | |
152 if (!domain.empty()) { | |
153 GURL::Replacements rep; | |
154 rep.SetHostStr(domain); | |
155 site = site.ReplaceComponents(rep); | |
156 } | |
157 } | |
158 return site; | |
159 } | |
160 | |
161 /*static*/ | |
162 bool SiteInstance::IsSameWebSite(Profile* profile, | |
163 const GURL& real_url1, const GURL& real_url2) { | |
164 GURL url1 = GetEffectiveURL(profile, real_url1); | |
165 GURL url2 = GetEffectiveURL(profile, real_url2); | |
166 | |
167 // We infer web site boundaries based on the registered domain name of the | |
168 // top-level page and the scheme. We do not pay attention to the port if | |
169 // one is present, because pages served from different ports can still | |
170 // access each other if they change their document.domain variable. | |
171 | |
172 // Some special URLs will match the site instance of any other URL. This is | |
173 // done before checking both of them for validity, since we want these URLs | |
174 // to have the same site instance as even an invalid one. | |
175 if (IsURLSameAsAnySiteInstance(url1) || IsURLSameAsAnySiteInstance(url2)) | |
176 return true; | |
177 | |
178 // If either URL is invalid, they aren't part of the same site. | |
179 if (!url1.is_valid() || !url2.is_valid()) | |
180 return false; | |
181 | |
182 // If the schemes differ, they aren't part of the same site. | |
183 if (url1.scheme() != url2.scheme()) | |
184 return false; | |
185 | |
186 return net::RegistryControlledDomainService::SameDomainOrHost(url1, url2); | |
187 } | |
188 | |
189 /*static*/ | |
190 GURL SiteInstance::GetEffectiveURL(Profile* profile, const GURL& url) { | |
191 if (!profile || !profile->GetExtensionService()) | |
192 return url; | |
193 | |
194 const Extension* extension = | |
195 profile->GetExtensionService()->GetExtensionByWebExtent(url); | |
196 if (extension) { | |
197 // If the URL is part of an extension's web extent, convert it to an | |
198 // extension URL. | |
199 return extension->GetResourceURL(url.path()); | |
200 } else { | |
201 return url; | |
202 } | |
203 } | |
204 | |
205 /*static*/ | |
206 RenderProcessHost::Type SiteInstance::RendererTypeForURL(const GURL& url) { | |
207 if (!url.is_valid()) | |
208 return RenderProcessHost::TYPE_NORMAL; | |
209 | |
210 if (url.SchemeIs(chrome::kExtensionScheme)) | |
211 return RenderProcessHost::TYPE_EXTENSION; | |
212 | |
213 // TODO(erikkay) creis recommends using UseWebUIForURL instead. | |
214 if (WebUIFactory::HasWebUIScheme(url)) | |
215 return RenderProcessHost::TYPE_WEBUI; | |
216 | |
217 return RenderProcessHost::TYPE_NORMAL; | |
218 } | |
219 | |
220 RenderProcessHost::Type SiteInstance::GetRendererType() { | |
221 // We may not have a site at this point, which generally means this is a | |
222 // normal navigation. | |
223 if (!has_site_) | |
224 return RenderProcessHost::TYPE_NORMAL; | |
225 | |
226 return RendererTypeForURL(site_); | |
227 } | |
228 | |
229 void SiteInstance::Observe(NotificationType type, | |
230 const NotificationSource& source, | |
231 const NotificationDetails& details) { | |
232 DCHECK(type == NotificationType::RENDERER_PROCESS_TERMINATED); | |
233 RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr(); | |
234 if (rph == process_) | |
235 process_ = NULL; | |
236 } | |
OLD | NEW |