blob: a42c790eee98942215f1845b4f49159c42823080 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 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.
initial.commit09911bf2008-07-26 23:55:294
5#include "chrome/browser/site_instance.h"
6
7#include "net/base/registry_controlled_domain.h"
8
9SiteInstance::~SiteInstance() {
10 // Now that no one is referencing us, we can safely remove ourselves from
11 // the BrowsingInstance. Any future visits to a page from this site
12 // (within the same BrowsingInstance) can safely create a new SiteInstance.
13 if (has_site_)
14 browsing_instance_->UnregisterSiteInstance(this);
15}
16
17RenderProcessHost* SiteInstance::GetProcess() {
18 RenderProcessHost* process = NULL;
19 if (process_host_id_ != -1)
20 process = RenderProcessHost::FromID(process_host_id_);
21
22 // Create a new process if ours went away or was reused.
23 if (!process) {
24 // See if we should reuse an old process
25 if (RenderProcessHost::ShouldTryToUseExistingProcessHost())
26 process = RenderProcessHost::GetExistingProcessHost(
27 browsing_instance_->profile());
28
29 // Otherwise (or if that fails), create a new one.
30 if (!process)
31 process = new RenderProcessHost(browsing_instance_->profile());
32
33 // Update our host ID, so all pages in this SiteInstance will use
34 // the correct process.
35 process_host_id_ = process->host_id();
36
37 // Make sure the process starts at the right max_page_id
38 process->UpdateMaxPageID(max_page_id_);
39 }
40 DCHECK(process);
41
42 return process;
43}
44
45void SiteInstance::SetSite(const GURL& url) {
46 // A SiteInstance's site should not change.
47 // TODO(creis): When following links or script navigations, we can currently
48 // render pages from other sites in this SiteInstance. This will eventually
49 // be fixed, but until then, we should still not set the site of a
50 // SiteInstance more than once.
51 DCHECK(!has_site_);
52
53 // Remember that this SiteInstance has been used to load a URL, even if the
54 // URL is invalid.
55 has_site_ = true;
56 site_ = GetSiteForURL(url);
57
58 // Now that we have a site, register it with the BrowsingInstance. This
59 // ensures that we won't create another SiteInstance for this site within
60 // the same BrowsingInstance, because all same-site pages within a
61 // BrowsingInstance can script each other.
62 browsing_instance_->RegisterSiteInstance(this);
63}
64
65bool SiteInstance::HasRelatedSiteInstance(const GURL& url) {
66 return browsing_instance_->HasSiteInstance(url);
67}
68
69SiteInstance* SiteInstance::GetRelatedSiteInstance(const GURL& url) {
70 return browsing_instance_->GetSiteInstanceForURL(url);
71}
72
73/*static*/
74SiteInstance* SiteInstance::CreateSiteInstance(Profile* profile) {
75 return new SiteInstance(new BrowsingInstance(profile));
76}
77
78/*static*/
79GURL SiteInstance::GetSiteForURL(const GURL& url) {
80 // URLs with no host should have an empty site.
81 GURL site;
82
83 // TODO(creis): For many protocols, we should just treat the scheme as the
84 // site, since there is no host. e.g., file:, about:, chrome-resource:
85
86 // If the url has a host, then determine the site.
87 if (url.has_host()) {
88 // Only keep the scheme, registered domain, and port as given by GetOrigin.
89 site = url.GetOrigin();
90
91 // If this URL has a registered domain, we only want to remember that part.
92 std::string domain =
[email protected]8ac1a752008-07-31 19:40:3793 net::RegistryControlledDomainService::GetDomainAndRegistry(url);
initial.commit09911bf2008-07-26 23:55:2994 if (!domain.empty()) {
95 GURL::Replacements rep;
96 rep.SetHostStr(domain);
97 site = site.ReplaceComponents(rep);
98 }
99 }
100 return site;
101}
102
103/*static*/
104bool SiteInstance::IsSameWebSite(const GURL& url1, const GURL& url2) {
105 // We infer web site boundaries based on the registered domain name of the
106 // top-level page, as well as the scheme and the port.
107
108 // We must treat javascript: URLs as part of the same site, regardless of
109 // the site.
110 if (url1.SchemeIs("javascript") || url2.SchemeIs("javascript"))
111 return true;
112
113 // We treat about:crash, about:hang, and about:shorthang as the same site as
114 // any URL, since they are used as demos for crashing/hanging a process.
115 GURL about_crash = GURL("about:crash");
116 GURL about_hang = GURL("about:hang");
117 GURL about_shorthang = GURL("about:shorthang");
118 if (url1 == about_crash || url2 == about_crash ||
119 url1 == about_hang || url2 == about_hang ||
120 url1 == about_shorthang || url2 == about_shorthang)
121 return true;
122
123 // If either URL is invalid, they aren't part of the same site.
124 if (!url1.is_valid() || !url2.is_valid()) {
125 return false;
126 }
127
128 // If the scheme or port differ, they aren't part of the same site.
129 if (url1.scheme() != url2.scheme() || url1.port() != url2.port()) {
130 return false;
131 }
132
[email protected]8ac1a752008-07-31 19:40:37133 return net::RegistryControlledDomainService::SameDomainOrHost(url1, url2);
initial.commit09911bf2008-07-26 23:55:29134}
license.botbf09a502008-08-24 00:55:55135