By moving the call to Load() up in SearchProvider::Start(), we are giving a chance...
[chromium-blink-merge.git] / content / browser / site_instance_impl.cc
blob14e1651adfdf82c530934da8e60622e6505e107b
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
3 // found in the LICENSE file.
5 #include "content/browser/site_instance_impl.h"
7 #include "base/command_line.h"
8 #include "content/browser/browsing_instance.h"
9 #include "content/browser/child_process_security_policy_impl.h"
10 #include "content/browser/renderer_host/render_process_host_impl.h"
11 #include "content/browser/storage_partition_impl.h"
12 #include "content/public/browser/content_browser_client.h"
13 #include "content/public/browser/notification_service.h"
14 #include "content/public/browser/notification_types.h"
15 #include "content/public/browser/render_process_host_factory.h"
16 #include "content/public/browser/web_ui_controller_factory.h"
17 #include "content/public/common/content_switches.h"
18 #include "content/public/common/url_constants.h"
19 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
21 namespace content {
23 static bool IsURLSameAsAnySiteInstance(const GURL& url) {
24 if (!url.is_valid())
25 return false;
27 // We treat javascript: as the same site as any URL since it is actually
28 // a modifier on existing pages.
29 if (url.SchemeIs(chrome::kJavaScriptScheme))
30 return true;
32 return url == GURL(chrome::kChromeUICrashURL) ||
33 url == GURL(chrome::kChromeUIKillURL) ||
34 url == GURL(chrome::kChromeUIHangURL) ||
35 url == GURL(kChromeUIShorthangURL);
38 int32 SiteInstanceImpl::next_site_instance_id_ = 1;
40 SiteInstanceImpl::SiteInstanceImpl(BrowsingInstance* browsing_instance)
41 : id_(next_site_instance_id_++),
42 browsing_instance_(browsing_instance),
43 render_process_host_factory_(NULL),
44 process_(NULL),
45 has_site_(false) {
46 DCHECK(browsing_instance);
48 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_TERMINATED,
49 NotificationService::AllBrowserContextsAndSources());
52 SiteInstanceImpl::~SiteInstanceImpl() {
53 GetContentClient()->browser()->SiteInstanceDeleting(this);
55 // Now that no one is referencing us, we can safely remove ourselves from
56 // the BrowsingInstance. Any future visits to a page from this site
57 // (within the same BrowsingInstance) can safely create a new SiteInstance.
58 if (has_site_)
59 browsing_instance_->UnregisterSiteInstance(
60 static_cast<SiteInstance*>(this));
63 int32 SiteInstanceImpl::GetId() {
64 return id_;
67 bool SiteInstanceImpl::HasProcess() const {
68 if (process_ != NULL)
69 return true;
71 // If we would use process-per-site for this site, also check if there is an
72 // existing process that we would use if GetProcess() were called.
73 BrowserContext* browser_context =
74 browsing_instance_->browser_context();
75 if (has_site_ &&
76 RenderProcessHostImpl::ShouldUseProcessPerSite(browser_context, site_) &&
77 RenderProcessHostImpl::GetProcessHostForSite(browser_context, site_)) {
78 return true;
81 return false;
84 RenderProcessHost* SiteInstanceImpl::GetProcess() {
85 // TODO(erikkay) It would be nice to ensure that the renderer type had been
86 // properly set before we get here. The default tab creation case winds up
87 // with no site set at this point, so it will default to TYPE_NORMAL. This
88 // may not be correct, so we'll wind up potentially creating a process that
89 // we then throw away, or worse sharing a process with the wrong process type.
90 // See crbug.com/43448.
92 // Create a new process if ours went away or was reused.
93 if (!process_) {
94 BrowserContext* browser_context = browsing_instance_->browser_context();
96 // If we should use process-per-site mode (either in general or for the
97 // given site), then look for an existing RenderProcessHost for the site.
98 bool use_process_per_site = has_site_ &&
99 RenderProcessHostImpl::ShouldUseProcessPerSite(browser_context, site_);
100 if (use_process_per_site) {
101 process_ = RenderProcessHostImpl::GetProcessHostForSite(browser_context,
102 site_);
105 // If not (or if none found), see if we should reuse an existing process.
106 if (!process_ && RenderProcessHostImpl::ShouldTryToUseExistingProcessHost(
107 browser_context, site_)) {
108 process_ = RenderProcessHostImpl::GetExistingProcessHost(browser_context,
109 site_);
112 // Otherwise (or if that fails), create a new one.
113 if (!process_) {
114 if (render_process_host_factory_) {
115 process_ = render_process_host_factory_->CreateRenderProcessHost(
116 browser_context);
117 } else {
118 StoragePartitionImpl* partition =
119 static_cast<StoragePartitionImpl*>(
120 BrowserContext::GetStoragePartition(browser_context, this));
121 process_ =
122 new RenderProcessHostImpl(browser_context, partition,
123 site_.SchemeIs(chrome::kGuestScheme));
126 CHECK(process_);
128 // If we are using process-per-site, we need to register this process
129 // for the current site so that we can find it again. (If no site is set
130 // at this time, we will register it in SetSite().)
131 if (use_process_per_site) {
132 RenderProcessHostImpl::RegisterProcessHostForSite(browser_context,
133 process_, site_);
136 GetContentClient()->browser()->SiteInstanceGotProcess(this);
138 if (has_site_)
139 LockToOrigin();
141 DCHECK(process_);
143 return process_;
146 void SiteInstanceImpl::SetSite(const GURL& url) {
147 // A SiteInstance's site should not change.
148 // TODO(creis): When following links or script navigations, we can currently
149 // render pages from other sites in this SiteInstance. This will eventually
150 // be fixed, but until then, we should still not set the site of a
151 // SiteInstance more than once.
152 DCHECK(!has_site_);
154 // Remember that this SiteInstance has been used to load a URL, even if the
155 // URL is invalid.
156 has_site_ = true;
157 BrowserContext* browser_context = browsing_instance_->browser_context();
158 site_ = GetSiteForURL(browser_context, url);
160 // Now that we have a site, register it with the BrowsingInstance. This
161 // ensures that we won't create another SiteInstance for this site within
162 // the same BrowsingInstance, because all same-site pages within a
163 // BrowsingInstance can script each other.
164 browsing_instance_->RegisterSiteInstance(this);
166 if (process_) {
167 LockToOrigin();
169 // Ensure the process is registered for this site if necessary.
170 if (RenderProcessHostImpl::ShouldUseProcessPerSite(browser_context,
171 site_)) {
172 RenderProcessHostImpl::RegisterProcessHostForSite(
173 browser_context, process_, site_);
178 const GURL& SiteInstanceImpl::GetSiteURL() const {
179 return site_;
182 bool SiteInstanceImpl::HasSite() const {
183 return has_site_;
186 bool SiteInstanceImpl::HasRelatedSiteInstance(const GURL& url) {
187 return browsing_instance_->HasSiteInstance(url);
190 SiteInstance* SiteInstanceImpl::GetRelatedSiteInstance(const GURL& url) {
191 return browsing_instance_->GetSiteInstanceForURL(url);
194 bool SiteInstanceImpl::IsRelatedSiteInstance(const SiteInstance* instance) {
195 return browsing_instance_ ==
196 static_cast<const SiteInstanceImpl*>(instance)->browsing_instance_;
199 bool SiteInstanceImpl::HasWrongProcessForURL(const GURL& url) {
200 // Having no process isn't a problem, since we'll assign it correctly.
201 // Note that HasProcess() may return true if process_ is null, in
202 // process-per-site cases where there's an existing process available.
203 // We want to use such a process in the IsSuitableHost check, so we
204 // may end up assigning process_ in the GetProcess() call below.
205 if (!HasProcess())
206 return false;
208 // If the URL to navigate to can be associated with any site instance,
209 // we want to keep it in the same process.
210 if (IsURLSameAsAnySiteInstance(url))
211 return false;
213 // If the site URL is an extension (e.g., for hosted apps or WebUI) but the
214 // process is not (or vice versa), make sure we notice and fix it.
215 GURL site_url = GetSiteForURL(browsing_instance_->browser_context(), url);
216 return !RenderProcessHostImpl::IsSuitableHost(
217 GetProcess(), browsing_instance_->browser_context(), site_url);
220 BrowserContext* SiteInstanceImpl::GetBrowserContext() const {
221 return browsing_instance_->browser_context();
224 /*static*/
225 SiteInstance* SiteInstance::Create(BrowserContext* browser_context) {
226 return new SiteInstanceImpl(new BrowsingInstance(browser_context));
229 /*static*/
230 SiteInstance* SiteInstance::CreateForURL(BrowserContext* browser_context,
231 const GURL& url) {
232 // This BrowsingInstance may be deleted if it returns an existing
233 // SiteInstance.
234 scoped_refptr<BrowsingInstance> instance(
235 new BrowsingInstance(browser_context));
236 return instance->GetSiteInstanceForURL(url);
239 /*static*/
240 bool SiteInstance::IsSameWebSite(BrowserContext* browser_context,
241 const GURL& real_url1,
242 const GURL& real_url2) {
243 GURL url1 = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url1);
244 GURL url2 = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url2);
246 // We infer web site boundaries based on the registered domain name of the
247 // top-level page and the scheme. We do not pay attention to the port if
248 // one is present, because pages served from different ports can still
249 // access each other if they change their document.domain variable.
251 // Some special URLs will match the site instance of any other URL. This is
252 // done before checking both of them for validity, since we want these URLs
253 // to have the same site instance as even an invalid one.
254 if (IsURLSameAsAnySiteInstance(url1) || IsURLSameAsAnySiteInstance(url2))
255 return true;
257 // If either URL is invalid, they aren't part of the same site.
258 if (!url1.is_valid() || !url2.is_valid())
259 return false;
261 // If the schemes differ, they aren't part of the same site.
262 if (url1.scheme() != url2.scheme())
263 return false;
265 return net::RegistryControlledDomainService::SameDomainOrHost(url1, url2);
268 /*static*/
269 GURL SiteInstance::GetSiteForURL(BrowserContext* browser_context,
270 const GURL& real_url) {
271 // TODO(fsamuel, creis): For some reason appID is not recognized as a host.
272 if (real_url.SchemeIs(chrome::kGuestScheme))
273 return real_url;
275 GURL url = SiteInstanceImpl::GetEffectiveURL(browser_context, real_url);
277 // URLs with no host should have an empty site.
278 GURL site;
280 // TODO(creis): For many protocols, we should just treat the scheme as the
281 // site, since there is no host. e.g., file:, about:, chrome:
283 // If the url has a host, then determine the site.
284 if (url.has_host()) {
285 // Only keep the scheme and registered domain as given by GetOrigin. This
286 // may also include a port, which we need to drop.
287 site = url.GetOrigin();
289 // Remove port, if any.
290 if (site.has_port()) {
291 GURL::Replacements rep;
292 rep.ClearPort();
293 site = site.ReplaceComponents(rep);
296 // If this URL has a registered domain, we only want to remember that part.
297 std::string domain =
298 net::RegistryControlledDomainService::GetDomainAndRegistry(url);
299 if (!domain.empty()) {
300 GURL::Replacements rep;
301 rep.SetHostStr(domain);
302 site = site.ReplaceComponents(rep);
305 return site;
308 /*static*/
309 GURL SiteInstanceImpl::GetEffectiveURL(BrowserContext* browser_context,
310 const GURL& url) {
311 return GetContentClient()->browser()->
312 GetEffectiveURL(browser_context, url);
315 void SiteInstanceImpl::Observe(int type,
316 const NotificationSource& source,
317 const NotificationDetails& details) {
318 DCHECK(type == NOTIFICATION_RENDERER_PROCESS_TERMINATED);
319 RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr();
320 if (rph == process_)
321 process_ = NULL;
324 void SiteInstanceImpl::LockToOrigin() {
325 // We currently only restrict this process to a particular site if the
326 // --enable-strict-site-isolation or --site-per-process flags are present.
327 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
328 if (command_line.HasSwitch(switches::kEnableStrictSiteIsolation) ||
329 command_line.HasSwitch(switches::kSitePerProcess)) {
330 ChildProcessSecurityPolicyImpl* policy =
331 ChildProcessSecurityPolicyImpl::GetInstance();
332 policy->LockToOrigin(process_->GetID(), site_);
336 } // namespace content