OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_SITE_INSTANCE_H_ | |
6 #define CONTENT_BROWSER_RENDERER_HOST_SITE_INSTANCE_H_ | |
7 #pragma once | |
8 | |
9 #include "content/browser/renderer_host/render_process_host_impl.h" | |
10 #include "content/common/content_export.h" | |
11 #include "content/public/browser/notification_observer.h" | |
12 #include "content/public/browser/notification_registrar.h" | |
13 #include "googleurl/src/gurl.h" | |
14 | |
15 class BrowsingInstance; | |
16 | |
17 namespace content { | |
18 class BrowserContext; | |
19 class RenderProcessHostFactory; | |
20 } | |
21 | |
22 /////////////////////////////////////////////////////////////////////////////// | |
23 // | |
24 // SiteInstance class | |
25 // | |
26 // A SiteInstance is a data structure that is associated with all pages in a | |
27 // given instance of a web site. Here, a web site is identified by its | |
28 // registered domain name and scheme. An instance includes all pages | |
29 // that are connected (i.e., either a user or a script navigated from one | |
30 // to the other). We represent instances using the BrowsingInstance class. | |
31 // | |
32 // In --process-per-tab, one SiteInstance is created for each tab (i.e., in the | |
33 // TabContents constructor), unless the tab is created by script (i.e., in | |
34 // TabContents::CreateNewView). This corresponds to one process per | |
35 // BrowsingInstance. | |
36 // | |
37 // In process-per-site-instance (the current default process model), | |
38 // SiteInstances are created (1) when the user manually creates a new tab | |
39 // (which also creates a new BrowsingInstance), and (2) when the user navigates | |
40 // across site boundaries (which uses the same BrowsingInstance). If the user | |
41 // navigates within a site, or opens links in new tabs within a site, the same | |
42 // SiteInstance is used. | |
43 // | |
44 // In --process-per-site, we consolidate all SiteInstances for a given site, | |
45 // throughout the entire browser context. This ensures that only one process | |
46 // will be dedicated to each site. | |
47 // | |
48 // Each NavigationEntry for a TabContents points to the SiteInstance that | |
49 // rendered it. Each RenderViewHost also points to the SiteInstance that it is | |
50 // associated with. A SiteInstance keeps track of the number of these | |
51 // references and deletes itself when the count goes to zero. This means that | |
52 // a SiteInstance is only live as long as it is accessible, either from new | |
53 // tabs with no NavigationEntries or in NavigationEntries in the history. | |
54 // | |
55 /////////////////////////////////////////////////////////////////////////////// | |
56 class CONTENT_EXPORT SiteInstance : public base::RefCounted<SiteInstance>, | |
57 public content::NotificationObserver { | |
58 public: | |
59 // Returns a unique ID for this SiteInstance. | |
60 int32 id() { return id_; } | |
61 | |
62 // Sets the factory used to create new RenderProcessHosts. This will also be | |
63 // passed on to SiteInstances spawned by this one. | |
64 // | |
65 // The factory must outlive the SiteInstance; ownership is not transferred. It | |
66 // may be NULL, in which case the default BrowserRenderProcessHost will be | |
67 // created (this is the behavior if you don't call this function). | |
68 void set_render_process_host_factory( | |
69 content::RenderProcessHostFactory* rph_factory) { | |
70 render_process_host_factory_ = rph_factory; | |
71 } | |
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 content::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 // Returns whether this SiteInstance has a process that is the wrong type for | |
106 // the given URL. If so, the browser should force a process swap when | |
107 // navigating to the URL. | |
108 bool HasWrongProcessForURL(const GURL& url) const; | |
109 | |
110 // Browser context to which this SiteInstance (and all related | |
111 // SiteInstances) belongs. | |
112 content::BrowserContext* GetBrowserContext() const; | |
113 | |
114 // Factory method to create a new SiteInstance. This will create a new | |
115 // new BrowsingInstance, so it should only be used when creating a new tab | |
116 // from scratch (or similar circumstances). Callers should ensure that | |
117 // this SiteInstance becomes ref counted, by storing it in a scoped_refptr. | |
118 // | |
119 // The render process host factory may be NULL. See SiteInstance constructor. | |
120 // | |
121 // TODO(creis): This may be an argument to build a pass_refptr<T> class, as | |
122 // Darin suggests. | |
123 static SiteInstance* CreateSiteInstance( | |
124 content::BrowserContext* browser_context); | |
125 | |
126 // Factory method to get the appropriate SiteInstance for the given URL, in | |
127 // a new BrowsingInstance. Use this instead of CreateSiteInstance when you | |
128 // know the URL, since it allows special site grouping rules to be applied | |
129 // (for example, to group chrome-ui pages into the same instance). | |
130 static SiteInstance* CreateSiteInstanceForURL( | |
131 content::BrowserContext* browser_context, const GURL& url); | |
132 | |
133 // Returns the site for the given URL, which includes only the scheme and | |
134 // registered domain. Returns an empty GURL if the URL has no host. | |
135 static GURL GetSiteForURL(content::BrowserContext* context, const GURL& url); | |
136 | |
137 // Return whether both URLs are part of the same web site, for the purpose of | |
138 // assigning them to processes accordingly. The decision is currently based | |
139 // on the registered domain of the URLs (google.com, bbc.co.uk), as well as | |
140 // the scheme (https, http). This ensures that two pages will be in | |
141 // the same process if they can communicate with other via JavaScript. | |
142 // (e.g., docs.google.com and mail.google.com have DOM access to each other | |
143 // if they both set their document.domain properties to google.com.) | |
144 static bool IsSameWebSite(content::BrowserContext* browser_context, | |
145 const GURL& url1, const GURL& url2); | |
146 | |
147 protected: | |
148 friend class base::RefCounted<SiteInstance>; | |
149 friend class BrowsingInstance; | |
150 | |
151 // Virtual to allow tests to extend it. | |
152 virtual ~SiteInstance(); | |
153 | |
154 // Create a new SiteInstance. Protected to give access to BrowsingInstance | |
155 // and tests; most callers should use CreateSiteInstance or | |
156 // GetRelatedSiteInstance instead. | |
157 explicit SiteInstance(BrowsingInstance* browsing_instance); | |
158 | |
159 // Get the effective URL for the given actual URL. | |
160 static GURL GetEffectiveURL(content::BrowserContext* browser_context, | |
161 const GURL& url); | |
162 | |
163 private: | |
164 // content::NotificationObserver implementation. | |
165 virtual void Observe(int type, | |
166 const content::NotificationSource& source, | |
167 const content::NotificationDetails& details) OVERRIDE; | |
168 | |
169 // Used to restrict a process' origin access rights. | |
170 void LockToOrigin(); | |
171 | |
172 // The next available SiteInstance ID. | |
173 static int32 next_site_instance_id_; | |
174 | |
175 // A unique ID for this SiteInstance. | |
176 int32 id_; | |
177 | |
178 content::NotificationRegistrar registrar_; | |
179 | |
180 // BrowsingInstance to which this SiteInstance belongs. | |
181 scoped_refptr<BrowsingInstance> browsing_instance_; | |
182 | |
183 // Factory for new RenderProcessHosts, not owned by this class. NULL indiactes | |
184 // that the default BrowserRenderProcessHost should be created. | |
185 const content::RenderProcessHostFactory* render_process_host_factory_; | |
186 | |
187 // Current RenderProcessHost that is rendering pages for this SiteInstance. | |
188 // This pointer will only change once the RenderProcessHost is destructed. It | |
189 // will still remain the same even if the process crashes, since in that | |
190 // scenario the RenderProcessHost remains the same. | |
191 content::RenderProcessHost* process_; | |
192 | |
193 // The web site that this SiteInstance is rendering pages for. | |
194 GURL site_; | |
195 | |
196 // Whether SetSite has been called. | |
197 bool has_site_; | |
198 | |
199 FRIEND_TEST_ALL_PREFIXES(RenderViewHostManagerTest, NewTabPageProcesses); | |
200 | |
201 DISALLOW_COPY_AND_ASSIGN(SiteInstance); | |
202 }; | |
203 | |
204 #endif // CONTENT_BROWSER_RENDERER_HOST_SITE_INSTANCE_H_ | |
OLD | NEW |