OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 CONTENT_BROWSER_RENDERER_HOST_SITE_INSTANCE_H_ | 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_SITE_INSTANCE_H_ |
jam
2012/01/24 03:29:33
nit: update header guard
ananta
2012/01/24 23:46:26
Done.
| |
6 #define CONTENT_BROWSER_RENDERER_HOST_SITE_INSTANCE_H_ | 6 #define CONTENT_BROWSER_RENDERER_HOST_SITE_INSTANCE_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "content/browser/renderer_host/render_process_host_impl.h" | 9 #include "content/browser/renderer_host/render_process_host_impl.h" |
10 #include "content/common/content_export.h" | 10 #include "content/common/content_export.h" |
11 #include "content/public/browser/notification_observer.h" | 11 #include "content/public/browser/notification_observer.h" |
12 #include "content/public/browser/notification_registrar.h" | 12 #include "content/public/browser/notification_registrar.h" |
13 #include "content/public/browser/site_instance.h" | |
13 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
14 | 15 |
15 class BrowsingInstance; | |
16 | |
17 namespace content { | 16 namespace content { |
18 class BrowserContext; | |
19 class RenderProcessHostFactory; | 17 class RenderProcessHostFactory; |
20 } | 18 } |
21 | 19 |
22 /////////////////////////////////////////////////////////////////////////////// | 20 /////////////////////////////////////////////////////////////////////////////// |
23 // | 21 // |
24 // SiteInstance class | 22 // SiteInstanceImpl class |
25 // | 23 // |
26 // A SiteInstance is a data structure that is associated with all pages in a | 24 // 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 | 25 // 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 | 26 // 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 | 27 // 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. | 28 // to the other). We represent instances using the BrowsingInstance class. |
31 // | 29 // |
32 // In --process-per-tab, one SiteInstance is created for each tab (i.e., in the | 30 // 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 | 31 // TabContents constructor), unless the tab is created by script (i.e., in |
34 // TabContents::CreateNewView). This corresponds to one process per | 32 // TabContents::CreateNewView). This corresponds to one process per |
(...skipping 11 matching lines...) Expand all Loading... | |
46 // will be dedicated to each site. | 44 // will be dedicated to each site. |
47 // | 45 // |
48 // Each NavigationEntry for a TabContents points to the SiteInstance that | 46 // Each NavigationEntry for a TabContents points to the SiteInstance that |
49 // rendered it. Each RenderViewHost also points to the SiteInstance that it is | 47 // rendered it. Each RenderViewHost also points to the SiteInstance that it is |
50 // associated with. A SiteInstance keeps track of the number of these | 48 // 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 | 49 // 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 | 50 // 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. | 51 // tabs with no NavigationEntries or in NavigationEntries in the history. |
54 // | 52 // |
55 /////////////////////////////////////////////////////////////////////////////// | 53 /////////////////////////////////////////////////////////////////////////////// |
56 class CONTENT_EXPORT SiteInstance : public base::RefCounted<SiteInstance>, | 54 class CONTENT_EXPORT SiteInstanceImpl : public content::SiteInstance, |
57 public content::NotificationObserver { | 55 public content::NotificationObserver { |
58 public: | 56 public: |
59 // Returns a unique ID for this SiteInstance. | 57 // content::SiteInstance interface overrides. |
60 int32 id() { return id_; } | 58 virtual int32 GetId() OVERRIDE; |
59 virtual bool HasProcess() const OVERRIDE; | |
60 virtual content::RenderProcessHost* GetProcess() OVERRIDE; | |
61 virtual void SetSite(const GURL& url) OVERRIDE; | |
62 virtual const GURL& GetSite() const OVERRIDE; | |
63 virtual bool HasSite() const OVERRIDE; | |
64 virtual bool HasRelatedSiteInstance(const GURL& url) OVERRIDE; | |
65 virtual SiteInstance* GetRelatedSiteInstance(const GURL& url) OVERRIDE; | |
66 virtual bool HasWrongProcessForURL(const GURL& url) const OVERRIDE; | |
jam
2012/01/24 03:29:33
nit extra space
ananta
2012/01/24 23:46:26
Done.
| |
67 virtual content::BrowserContext* GetBrowserContext() const OVERRIDE; | |
68 virtual BrowsingInstance* GetBrowsingInstance() const OVERRIDE; | |
61 | 69 |
62 // Sets the factory used to create new RenderProcessHosts. This will also be | 70 // Sets the factory used to create new RenderProcessHosts. This will also be |
63 // passed on to SiteInstances spawned by this one. | 71 // passed on to SiteInstances spawned by this one. |
64 // | 72 // |
65 // The factory must outlive the SiteInstance; ownership is not transferred. It | 73 // The factory must outlive the SiteInstance; ownership is not transferred. It |
66 // may be NULL, in which case the default BrowserRenderProcessHost will be | 74 // may be NULL, in which case the default BrowserRenderProcessHost will be |
67 // created (this is the behavior if you don't call this function). | 75 // created (this is the behavior if you don't call this function). |
68 void set_render_process_host_factory( | 76 void set_render_process_host_factory( |
69 content::RenderProcessHostFactory* rph_factory) { | 77 content::RenderProcessHostFactory* rph_factory) { |
70 render_process_host_factory_ = rph_factory; | 78 render_process_host_factory_ = rph_factory; |
71 } | 79 } |
72 | 80 |
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: | 81 protected: |
148 friend class base::RefCounted<SiteInstance>; | |
149 friend class BrowsingInstance; | 82 friend class BrowsingInstance; |
83 friend class content::SiteInstance; | |
150 | 84 |
151 // Virtual to allow tests to extend it. | 85 // Virtual to allow tests to extend it. |
152 virtual ~SiteInstance(); | 86 virtual ~SiteInstanceImpl(); |
153 | 87 |
154 // Create a new SiteInstance. Protected to give access to BrowsingInstance | 88 // Create a new SiteInstance. Protected to give access to BrowsingInstance |
155 // and tests; most callers should use CreateSiteInstance or | 89 // and tests; most callers should use CreateSiteInstance or |
156 // GetRelatedSiteInstance instead. | 90 // GetRelatedSiteInstance instead. |
157 explicit SiteInstance(BrowsingInstance* browsing_instance); | 91 explicit SiteInstanceImpl(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 | 92 |
163 private: | 93 private: |
164 // content::NotificationObserver implementation. | 94 // content::NotificationObserver implementation. |
165 virtual void Observe(int type, | 95 virtual void Observe(int type, |
166 const content::NotificationSource& source, | 96 const content::NotificationSource& source, |
167 const content::NotificationDetails& details) OVERRIDE; | 97 const content::NotificationDetails& details) OVERRIDE; |
168 | 98 |
169 // Used to restrict a process' origin access rights. | 99 // Used to restrict a process' origin access rights. |
170 void LockToOrigin(); | 100 void LockToOrigin(); |
171 | 101 |
(...skipping 19 matching lines...) Expand all Loading... | |
191 content::RenderProcessHost* process_; | 121 content::RenderProcessHost* process_; |
192 | 122 |
193 // The web site that this SiteInstance is rendering pages for. | 123 // The web site that this SiteInstance is rendering pages for. |
194 GURL site_; | 124 GURL site_; |
195 | 125 |
196 // Whether SetSite has been called. | 126 // Whether SetSite has been called. |
197 bool has_site_; | 127 bool has_site_; |
198 | 128 |
199 FRIEND_TEST_ALL_PREFIXES(RenderViewHostManagerTest, NewTabPageProcesses); | 129 FRIEND_TEST_ALL_PREFIXES(RenderViewHostManagerTest, NewTabPageProcesses); |
200 | 130 |
201 DISALLOW_COPY_AND_ASSIGN(SiteInstance); | 131 DISALLOW_COPY_AND_ASSIGN(SiteInstanceImpl); |
202 }; | 132 }; |
203 | 133 |
204 #endif // CONTENT_BROWSER_RENDERER_HOST_SITE_INSTANCE_H_ | 134 #endif // CONTENT_BROWSER_RENDERER_HOST_SITE_INSTANCE_H_ |
OLD | NEW |