Fixing build: GetViewContainer changed name from under me. :)
[chromium-blink-merge.git] / chrome / browser / site_instance.h
blob904d52fce14fef5a907b7ccb05ce72286f08578d
1 // 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.
5 #ifndef CHROME_BROWSER_SITE_INSTANCE_H__
6 #define CHROME_BROWSER_SITE_INSTANCE_H__
8 #include "chrome/browser/browsing_instance.h"
9 #include "chrome/browser/render_process_host.h"
10 #include "googleurl/src/gurl.h"
12 ///////////////////////////////////////////////////////////////////////////////
14 // SiteInstance class
16 // A SiteInstance is a data structure that is associated with all pages in a
17 // given instance of a web site. Here, a web site is identified by its
18 // registered domain name, scheme, and port. An instance includes all pages
19 // that are connected (i.e., either a user or a script navigated from one
20 // to the other). We represent instances using the BrowsingInstance class.
22 // In --process-per-tab, one SiteInstance is created for each tab (i.e., in the
23 // WebContents constructor), unless the tab is created by script (i.e., in
24 // WebContents::CreateNewView). This corresponds to one process per
25 // BrowsingInstance.
27 // In process-per-site-instance (the current default process model),
28 // SiteInstances are created (1) when the user manually creates a new tab
29 // (which also creates a new BrowsingInstance), and (2) when the user navigates
30 // across site boundaries (which uses the same BrowsingInstance). If the user
31 // navigates within a site, or opens links in new tabs within a site, the same
32 // SiteInstance is used.
34 // In --process-per-site, we consolidate all SiteInstances for a given site,
35 // throughout the entire profile. This ensures that only one process will be
36 // dedicated to each site.
38 // Each NavigationEntry for a WebContents points to the SiteInstance that
39 // rendered it. Each RenderViewHost also points to the SiteInstance that it is
40 // associated with. A SiteInstance keeps track of the number of these
41 // references and deletes itself when the count goes to zero. This means that
42 // a SiteInstance is only live as long as it is accessible, either from new
43 // tabs with no NavigationEntries or in NavigationEntries in the history.
45 ///////////////////////////////////////////////////////////////////////////////
46 class SiteInstance : public base::RefCounted<SiteInstance> {
47 public:
48 // Virtual to allow tests to extend it.
49 virtual ~SiteInstance();
51 // Get the BrowsingInstance to which this SiteInstance belongs.
52 BrowsingInstance* browsing_instance() { return browsing_instance_; }
54 // Set / Get the host ID for this SiteInstance's current RenderProcessHost.
55 void set_process_host_id(int process_host_id) {
56 process_host_id_ = process_host_id;
58 int process_host_id() const { return process_host_id_; }
60 // Update / Get the max page ID for this SiteInstance.
61 void UpdateMaxPageID(int32 page_id) {
62 if (page_id > max_page_id_)
63 max_page_id_ = page_id;
65 int32 max_page_id() const { return max_page_id_; }
67 // Returns the current process being used to render pages in this
68 // SiteInstance. If the process has crashed or otherwise gone away, then
69 // this method will create a new process and update our host ID accordingly.
70 RenderProcessHost* GetProcess();
72 // Set / Get the web site that this SiteInstance is rendering pages for.
73 // This includes the scheme, registered domain, and port. If the URL does
74 // not have a valid registered domain, then the full hostname is stored.
75 void SetSite(const GURL& url);
76 const GURL& site() const { return site_; }
77 bool has_site() const { return has_site_; }
79 // Returns whether there is currently a related SiteInstance (registered with
80 // BrowsingInstance) for the site of the given url. If so, we should try to
81 // avoid dedicating an unused SiteInstance to it (e.g., in a new tab).
82 bool HasRelatedSiteInstance(const GURL& url);
84 // Gets a SiteInstance for the given URL that shares the current
85 // BrowsingInstance, creating a new SiteInstance if necessary. This ensures
86 // that a BrowsingInstance only has one SiteInstance per site, so that pages
87 // in a BrowsingInstance have the ability to script each other. Callers
88 // should ensure that this SiteInstance becomes ref counted, by storing it in
89 // a scoped_refptr. (By having this method, we can hide the BrowsingInstance
90 // class from the rest of the codebase.)
91 // TODO(creis): This may be an argument to build a pass_refptr<T> class, as
92 // Darin suggests.
93 SiteInstance* GetRelatedSiteInstance(const GURL& url);
95 // Factory method to create a new SiteInstance. This will create a new
96 // new BrowsingInstance, so it should only be used when creating a new tab
97 // from scratch (or similar circumstances). Callers should ensure that
98 // this SiteInstance becomes ref counted, by storing it in a scoped_refptr.
99 // TODO(creis): This may be an argument to build a pass_refptr<T> class, as
100 // Darin suggests.
101 static SiteInstance* CreateSiteInstance(Profile* profile);
103 // Returns the site for the given URL, which includes only the scheme,
104 // registered domain, and port. Returns an empty GURL if the URL has no
105 // host.
106 static GURL GetSiteForURL(const GURL& url);
108 // Return whether both URLs are part of the same web site, for the purpose of
109 // assigning them to processes accordingly. The decision is currently based
110 // on the registered domain of the URLs (google.com, bbc.co.uk), as well as
111 // the scheme (https, http) and port. This ensures that two pages will be in
112 // the same process if they can communicate with other via JavaScript.
113 // (e.g., docs.google.com and mail.google.com have DOM access to each other
114 // if they both set their document.domain properties to google.com.)
115 static bool IsSameWebSite(const GURL& url1, const GURL& url2);
117 protected:
118 friend class BrowsingInstance;
120 // Create a new SiteInstance. Protected to give access to BrowsingInstance
121 // and tests; most callers should use CreateSiteInstance or
122 // GetRelatedSiteInstance instead.
123 SiteInstance(BrowsingInstance* browsing_instance)
124 : browsing_instance_(browsing_instance),
125 process_host_id_(-1),
126 max_page_id_(-1),
127 has_site_(false) {
128 DCHECK(browsing_instance);
131 private:
132 // BrowsingInstance to which this SiteInstance belongs.
133 scoped_refptr<BrowsingInstance> browsing_instance_;
135 // Current host ID for the RenderProcessHost that is rendering pages for this
136 // SiteInstance. If the rendering process dies, this host ID can be
137 // replaced when a new process is created, without losing the association
138 // between all pages in this SiteInstance.
139 int process_host_id_;
141 // The current max_page_id in the SiteInstance's RenderProcessHost. If the
142 // rendering process dies, its replacement should start issuing page IDs that
143 // are larger than this value.
144 int32 max_page_id_;
146 // The web site that this SiteInstance is rendering pages for.
147 GURL site_;
149 // Whether SetSite has been called.
150 bool has_site_;
152 DISALLOW_EVIL_CONSTRUCTORS(SiteInstance);
155 #endif // CHROME_BROWSER_SITE_INSTANCE_H__