OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #ifndef CHROME_BROWSER_RENDERER_HOST_SITE_INSTANCE_H_ | 5 #ifndef CHROME_BROWSER_RENDERER_HOST_SITE_INSTANCE_H_ |
6 #define CHROME_BROWSER_RENDERER_HOST_SITE_INSTANCE_H_ | 6 #define CHROME_BROWSER_RENDERER_HOST_SITE_INSTANCE_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "chrome/browser/renderer_host/render_process_host.h" | 9 // TODO(jam): remove this file when all files have been converted. |
10 #include "chrome/common/notification_observer.h" | 10 #include "content/browser/site_instance.h" |
11 #include "chrome/common/notification_registrar.h" | |
12 #include "googleurl/src/gurl.h" | |
13 | |
14 class BrowsingInstance; | |
15 | |
16 /////////////////////////////////////////////////////////////////////////////// | |
17 // | |
18 // SiteInstance class | |
19 // | |
20 // A SiteInstance is a data structure that is associated with all pages in a | |
21 // given instance of a web site. Here, a web site is identified by its | |
22 // registered domain name and scheme. An instance includes all pages | |
23 // that are connected (i.e., either a user or a script navigated from one | |
24 // to the other). We represent instances using the BrowsingInstance class. | |
25 // | |
26 // In --process-per-tab, one SiteInstance is created for each tab (i.e., in the | |
27 // TabContents constructor), unless the tab is created by script (i.e., in | |
28 // TabContents::CreateNewView). This corresponds to one process per | |
29 // BrowsingInstance. | |
30 // | |
31 // In process-per-site-instance (the current default process model), | |
32 // SiteInstances are created (1) when the user manually creates a new tab | |
33 // (which also creates a new BrowsingInstance), and (2) when the user navigates | |
34 // across site boundaries (which uses the same BrowsingInstance). If the user | |
35 // navigates within a site, or opens links in new tabs within a site, the same | |
36 // SiteInstance is used. | |
37 // | |
38 // In --process-per-site, we consolidate all SiteInstances for a given site, | |
39 // throughout the entire profile. This ensures that only one process will be | |
40 // dedicated to each site. | |
41 // | |
42 // Each NavigationEntry for a TabContents points to the SiteInstance that | |
43 // rendered it. Each RenderViewHost also points to the SiteInstance that it is | |
44 // associated with. A SiteInstance keeps track of the number of these | |
45 // references and deletes itself when the count goes to zero. This means that | |
46 // a SiteInstance is only live as long as it is accessible, either from new | |
47 // tabs with no NavigationEntries or in NavigationEntries in the history. | |
48 // | |
49 /////////////////////////////////////////////////////////////////////////////// | |
50 class SiteInstance : public base::RefCounted<SiteInstance>, | |
51 public NotificationObserver { | |
52 public: | |
53 // Get the BrowsingInstance to which this SiteInstance belongs. | |
54 BrowsingInstance* browsing_instance() { return browsing_instance_; } | |
55 | |
56 // Sets the factory used to create new RenderProcessHosts. This will also be | |
57 // passed on to SiteInstances spawned by this one. | |
58 // | |
59 // The factory must outlive the SiteInstance; ownership is not transferred. It | |
60 // may be NULL, in which case the default BrowserRenderProcessHost will be | |
61 // created (this is the behavior if you don't call this function). | |
62 void set_render_process_host_factory(RenderProcessHostFactory* rph_factory) { | |
63 render_process_host_factory_ = rph_factory; | |
64 } | |
65 | |
66 // Update / Get the max page ID for this SiteInstance. | |
67 void UpdateMaxPageID(int32 page_id) { | |
68 if (page_id > max_page_id_) | |
69 max_page_id_ = page_id; | |
70 } | |
71 int32 max_page_id() const { return max_page_id_; } | |
72 | |
73 // Whether this SiteInstance has a running process associated with it. | |
74 bool HasProcess() const; | |
75 | |
76 // Returns the current process being used to render pages in this | |
77 // SiteInstance. If the process has crashed or otherwise gone away, then | |
78 // this method will create a new process and update our host ID accordingly. | |
79 RenderProcessHost* GetProcess(); | |
80 | |
81 // Set / Get the web site that this SiteInstance is rendering pages for. | |
82 // This includes the scheme and registered domain, but not the port. If the | |
83 // URL does not have a valid registered domain, then the full hostname is | |
84 // stored. | |
85 void SetSite(const GURL& url); | |
86 const GURL& site() const { return site_; } | |
87 bool has_site() const { return has_site_; } | |
88 | |
89 // Returns whether there is currently a related SiteInstance (registered with | |
90 // BrowsingInstance) for the site of the given url. If so, we should try to | |
91 // avoid dedicating an unused SiteInstance to it (e.g., in a new tab). | |
92 bool HasRelatedSiteInstance(const GURL& url); | |
93 | |
94 // Gets a SiteInstance for the given URL that shares the current | |
95 // BrowsingInstance, creating a new SiteInstance if necessary. This ensures | |
96 // that a BrowsingInstance only has one SiteInstance per site, so that pages | |
97 // in a BrowsingInstance have the ability to script each other. Callers | |
98 // should ensure that this SiteInstance becomes ref counted, by storing it in | |
99 // a scoped_refptr. (By having this method, we can hide the BrowsingInstance | |
100 // class from the rest of the codebase.) | |
101 // TODO(creis): This may be an argument to build a pass_refptr<T> class, as | |
102 // Darin suggests. | |
103 SiteInstance* GetRelatedSiteInstance(const GURL& url); | |
104 | |
105 // Factory method to create a new SiteInstance. This will create a new | |
106 // new BrowsingInstance, so it should only be used when creating a new tab | |
107 // from scratch (or similar circumstances). Callers should ensure that | |
108 // this SiteInstance becomes ref counted, by storing it in a scoped_refptr. | |
109 // | |
110 // The render process host factory may be NULL. See SiteInstance constructor. | |
111 // | |
112 // TODO(creis): This may be an argument to build a pass_refptr<T> class, as | |
113 // Darin suggests. | |
114 static SiteInstance* CreateSiteInstance(Profile* profile); | |
115 | |
116 // Factory method to get the appropriate SiteInstance for the given URL, in | |
117 // a new BrowsingInstance. Use this instead of CreateSiteInstance when you | |
118 // know the URL, since it allows special site grouping rules to be applied | |
119 // (for example, to group chrome-ui pages into the same instance). | |
120 static SiteInstance* CreateSiteInstanceForURL(Profile* profile, | |
121 const GURL& url); | |
122 | |
123 // Returns the site for the given URL, which includes only the scheme and | |
124 // registered domain. Returns an empty GURL if the URL has no host. | |
125 static GURL GetSiteForURL(Profile* profile, const GURL& url); | |
126 | |
127 // Return whether both URLs are part of the same web site, for the purpose of | |
128 // assigning them to processes accordingly. The decision is currently based | |
129 // on the registered domain of the URLs (google.com, bbc.co.uk), as well as | |
130 // the scheme (https, http). This ensures that two pages will be in | |
131 // the same process if they can communicate with other via JavaScript. | |
132 // (e.g., docs.google.com and mail.google.com have DOM access to each other | |
133 // if they both set their document.domain properties to google.com.) | |
134 static bool IsSameWebSite(Profile* profile, | |
135 const GURL& url1, const GURL& url2); | |
136 | |
137 // Returns the renderer type for this URL. | |
138 static RenderProcessHost::Type RendererTypeForURL(const GURL& url); | |
139 | |
140 protected: | |
141 friend class base::RefCounted<SiteInstance>; | |
142 friend class BrowsingInstance; | |
143 | |
144 // Virtual to allow tests to extend it. | |
145 virtual ~SiteInstance(); | |
146 | |
147 // Create a new SiteInstance. Protected to give access to BrowsingInstance | |
148 // and tests; most callers should use CreateSiteInstance or | |
149 // GetRelatedSiteInstance instead. | |
150 explicit SiteInstance(BrowsingInstance* browsing_instance); | |
151 | |
152 // Get the effective URL for the given actual URL. If the URL is part of an | |
153 // installed app, the effective URL is an extension URL with the ID of that | |
154 // extension as the host. This has the effect of grouping apps together in | |
155 // a common SiteInstance. | |
156 static GURL GetEffectiveURL(Profile* profile, const GURL& url); | |
157 | |
158 // Returns the type of renderer process this instance belongs in, for grouping | |
159 // purposes. | |
160 RenderProcessHost::Type GetRendererType(); | |
161 | |
162 private: | |
163 // NotificationObserver implementation. | |
164 virtual void Observe(NotificationType type, | |
165 const NotificationSource& source, | |
166 const NotificationDetails& details); | |
167 | |
168 NotificationRegistrar registrar_; | |
169 | |
170 // BrowsingInstance to which this SiteInstance belongs. | |
171 scoped_refptr<BrowsingInstance> browsing_instance_; | |
172 | |
173 // Factory for new RenderProcessHosts, not owned by this class. NULL indiactes | |
174 // that the default BrowserRenderProcessHost should be created. | |
175 const RenderProcessHostFactory* render_process_host_factory_; | |
176 | |
177 // Current RenderProcessHost that is rendering pages for this SiteInstance. | |
178 // This pointer will only change once the RenderProcessHost is destructed. It | |
179 // will still remain the same even if the process crashes, since in that | |
180 // scenario the RenderProcessHost remains the same. | |
181 RenderProcessHost* process_; | |
182 | |
183 // The current max_page_id in the SiteInstance's RenderProcessHost. If the | |
184 // rendering process dies, its replacement should start issuing page IDs that | |
185 // are larger than this value. | |
186 int32 max_page_id_; | |
187 | |
188 // The web site that this SiteInstance is rendering pages for. | |
189 GURL site_; | |
190 | |
191 // Whether SetSite has been called. | |
192 bool has_site_; | |
193 | |
194 DISALLOW_COPY_AND_ASSIGN(SiteInstance); | |
195 }; | |
196 | 11 |
197 #endif // CHROME_BROWSER_RENDERER_HOST_SITE_INSTANCE_H_ | 12 #endif // CHROME_BROWSER_RENDERER_HOST_SITE_INSTANCE_H_ |
OLD | NEW |