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_TAB_CONTENTS_RENDER_VIEW_HOST_MANAGER_H_ | |
6 #define CONTENT_BROWSER_TAB_CONTENTS_RENDER_VIEW_HOST_MANAGER_H_ | |
7 #pragma once | |
8 | |
9 #include "base/basictypes.h" | |
10 #include "base/logging.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "content/browser/site_instance_impl.h" | |
13 #include "content/common/content_export.h" | |
14 #include "content/public/browser/notification_observer.h" | |
15 #include "content/public/browser/notification_registrar.h" | |
16 #include "content/public/browser/render_view_host_delegate.h" | |
17 | |
18 class InterstitialPageImpl; | |
19 class NavigationControllerImpl; | |
20 class WebUIImpl; | |
21 | |
22 namespace content { | |
23 class BrowserContext; | |
24 class NavigationEntry; | |
25 class NavigationEntryImpl; | |
26 class RenderViewHost; | |
27 class RenderViewHostImpl; | |
28 class RenderWidgetHostView; | |
29 class TestWebContents; | |
30 } | |
31 | |
32 // Manages RenderViewHosts for a TabContents. Normally there is only one and | |
33 // it is easy to do. But we can also have transitions of processes (and hence | |
34 // RenderViewHosts) that can get complex. | |
35 class CONTENT_EXPORT RenderViewHostManager | |
36 : public content::RenderViewHostDelegate::RendererManagement, | |
37 public content::NotificationObserver { | |
38 public: | |
39 // Functions implemented by our owner that we need. | |
40 // | |
41 // TODO(brettw) Clean this up! These are all the functions in TabContents that | |
42 // are required to run this class. The design should probably be better such | |
43 // that these are more clear. | |
44 // | |
45 // There is additional complexity that some of the functions we need in | |
46 // TabContents are inherited and non-virtual. These are named with | |
47 // "RenderManager" so that the duplicate implementation of them will be clear. | |
48 class CONTENT_EXPORT Delegate { | |
49 public: | |
50 // Initializes the given renderer if necessary and creates the view ID | |
51 // corresponding to this view host. If this method is not called and the | |
52 // process is not shared, then the TabContents will act as though the | |
53 // renderer is not running (i.e., it will render "sad tab"). This method is | |
54 // automatically called from LoadURL. | |
55 // | |
56 // If you are attaching to an already-existing RenderView, you should call | |
57 // InitWithExistingID. | |
58 virtual bool CreateRenderViewForRenderManager( | |
59 content::RenderViewHost* render_view_host) = 0; | |
60 virtual void BeforeUnloadFiredFromRenderManager( | |
61 bool proceed, bool* proceed_to_fire_unload) = 0; | |
62 virtual void DidStartLoadingFromRenderManager( | |
63 content::RenderViewHost* render_view_host) = 0; | |
64 virtual void RenderViewGoneFromRenderManager( | |
65 content::RenderViewHost* render_view_host) = 0; | |
66 virtual void UpdateRenderViewSizeForRenderManager() = 0; | |
67 virtual void NotifySwappedFromRenderManager() = 0; | |
68 virtual NavigationControllerImpl& GetControllerForRenderManager() = 0; | |
69 | |
70 // Creates a WebUI object for the given URL if one applies. Ownership of the | |
71 // returned pointer will be passed to the caller. If no WebUI applies, | |
72 // returns NULL. | |
73 virtual WebUIImpl* CreateWebUIForRenderManager(const GURL& url) = 0; | |
74 | |
75 // Returns the navigation entry of the current navigation, or NULL if there | |
76 // is none. | |
77 virtual content::NavigationEntry* | |
78 GetLastCommittedNavigationEntryForRenderManager() = 0; | |
79 | |
80 // Returns true if the location bar should be focused by default rather than | |
81 // the page contents. The view calls this function when the tab is focused | |
82 // to see what it should do. | |
83 virtual bool FocusLocationBarByDefault() = 0; | |
84 | |
85 // Focuses the location bar. | |
86 virtual void SetFocusToLocationBar(bool select_all) = 0; | |
87 | |
88 // Creates a view and sets the size for the specified RVH. | |
89 virtual void CreateViewAndSetSizeForRVH(content::RenderViewHost* rvh) = 0; | |
90 | |
91 protected: | |
92 virtual ~Delegate() {} | |
93 }; | |
94 | |
95 // Both delegate pointers must be non-NULL and are not owned by this class. | |
96 // They must outlive this class. The RenderViewHostDelegate is what will be | |
97 // installed into all RenderViewHosts that are created. | |
98 // | |
99 // You must call Init() before using this class. | |
100 RenderViewHostManager(content::RenderViewHostDelegate* render_view_delegate, | |
101 Delegate* delegate); | |
102 virtual ~RenderViewHostManager(); | |
103 | |
104 // For arguments, see TabContents constructor. | |
105 void Init(content::BrowserContext* browser_context, | |
106 content::SiteInstance* site_instance, | |
107 int routing_id); | |
108 | |
109 // Returns the currently active RenderViewHost. | |
110 // | |
111 // This will be non-NULL between Init() and Shutdown(). You may want to NULL | |
112 // check it in many cases, however. Windows can send us messages during the | |
113 // destruction process after it has been shut down. | |
114 content::RenderViewHostImpl* current_host() const; | |
115 | |
116 // Returns the view associated with the current RenderViewHost, or NULL if | |
117 // there is no current one. | |
118 content::RenderWidgetHostView* GetRenderWidgetHostView() const; | |
119 | |
120 // Returns the pending render view host, or NULL if there is no pending one. | |
121 content::RenderViewHostImpl* pending_render_view_host() const; | |
122 | |
123 // Returns the current committed Web UI or NULL if none applies. | |
124 WebUIImpl* web_ui() const { return web_ui_.get(); } | |
125 | |
126 // Returns the Web UI for the pending navigation, or NULL of none applies. | |
127 WebUIImpl* pending_web_ui() const { return pending_web_ui_.get(); } | |
128 | |
129 // Called when we want to instruct the renderer to navigate to the given | |
130 // navigation entry. It may create a new RenderViewHost or re-use an existing | |
131 // one. The RenderViewHost to navigate will be returned. Returns NULL if one | |
132 // could not be created. | |
133 content::RenderViewHostImpl* Navigate( | |
134 const content::NavigationEntryImpl& entry); | |
135 | |
136 // Instructs the various live views to stop. Called when the user directed the | |
137 // page to stop loading. | |
138 void Stop(); | |
139 | |
140 // Notifies the regular and pending RenderViewHosts that a load is or is not | |
141 // happening. Even though the message is only for one of them, we don't know | |
142 // which one so we tell both. | |
143 void SetIsLoading(bool is_loading); | |
144 | |
145 // Whether to close the tab or not when there is a hang during an unload | |
146 // handler. If we are mid-crosssite navigation, then we should proceed | |
147 // with the navigation instead of closing the tab. | |
148 bool ShouldCloseTabOnUnresponsiveRenderer(); | |
149 | |
150 // Called when a renderer's main frame navigates. | |
151 void DidNavigateMainFrame(content::RenderViewHost* render_view_host); | |
152 | |
153 // Set the WebUI after committing a page load. This is useful for navigations | |
154 // initiated from a renderer, where we want to give the new renderer WebUI | |
155 // privileges from the originating renderer. | |
156 void SetWebUIPostCommit(WebUIImpl* web_ui); | |
157 | |
158 // Called when a provisional load on the given renderer is aborted. | |
159 void RendererAbortedProvisionalLoad( | |
160 content::RenderViewHost* render_view_host); | |
161 | |
162 // Sets the passed passed interstitial as the currently showing interstitial. | |
163 // |interstitial_page| should be non NULL (use the remove_interstitial_page | |
164 // method to unset the interstitial) and no interstitial page should be set | |
165 // when there is already a non NULL interstitial page set. | |
166 void set_interstitial_page(InterstitialPageImpl* interstitial_page) { | |
167 DCHECK(!interstitial_page_ && interstitial_page); | |
168 interstitial_page_ = interstitial_page; | |
169 } | |
170 | |
171 // Unsets the currently showing interstitial. | |
172 void remove_interstitial_page() { | |
173 DCHECK(interstitial_page_); | |
174 interstitial_page_ = NULL; | |
175 } | |
176 | |
177 // Returns the currently showing interstitial, NULL if no interstitial is | |
178 // showing. | |
179 InterstitialPageImpl* interstitial_page() const { | |
180 return interstitial_page_; | |
181 } | |
182 | |
183 // RenderViewHostDelegate::RendererManagement implementation. | |
184 virtual void ShouldClosePage( | |
185 bool for_cross_site_transition, | |
186 bool proceed, | |
187 const base::TimeTicks& proceed_time) OVERRIDE; | |
188 virtual void OnCrossSiteResponse(int new_render_process_host_id, | |
189 int new_request_id) OVERRIDE; | |
190 | |
191 // content::NotificationObserver implementation. | |
192 virtual void Observe(int type, | |
193 const content::NotificationSource& source, | |
194 const content::NotificationDetails& details) OVERRIDE; | |
195 | |
196 // Called when a RenderViewHost is about to be deleted. | |
197 void RenderViewDeleted(content::RenderViewHost* rvh); | |
198 | |
199 // Returns whether the given RenderViewHost is on the list of swapped out | |
200 // RenderViewHosts. | |
201 bool IsSwappedOut(content::RenderViewHost* rvh); | |
202 | |
203 private: | |
204 friend class content::TestWebContents; | |
205 friend class RenderViewHostManagerTest; | |
206 | |
207 // Returns whether this tab should transition to a new renderer for | |
208 // cross-site URLs. Enabled unless we see the --process-per-tab command line | |
209 // switch. Can be overridden in unit tests. | |
210 bool ShouldTransitionCrossSite(); | |
211 | |
212 // Returns true if the two navigation entries are incompatible in some way | |
213 // other than site instances. Cases where this can happen include Web UI | |
214 // to regular web pages. It will cause us to swap RenderViewHosts (and hence | |
215 // RenderProcessHosts) even if the site instance would otherwise be the same. | |
216 // As part of this, we'll also force new SiteInstances and BrowsingInstances. | |
217 // Either of the entries may be NULL. | |
218 bool ShouldSwapProcessesForNavigation( | |
219 const content::NavigationEntry* cur_entry, | |
220 const content::NavigationEntryImpl* new_entry) const; | |
221 | |
222 // Returns an appropriate SiteInstance object for the given NavigationEntry, | |
223 // possibly reusing the current SiteInstance. | |
224 // Never called if --process-per-tab is used. | |
225 content::SiteInstance* GetSiteInstanceForEntry( | |
226 const content::NavigationEntryImpl& entry, | |
227 content::SiteInstance* curr_instance); | |
228 | |
229 // Helper method to create a pending RenderViewHost for a cross-site | |
230 // navigation. | |
231 bool CreatePendingRenderView(const content::NavigationEntryImpl& entry, | |
232 content::SiteInstance* instance); | |
233 | |
234 // Sets up the necessary state for a new RenderViewHost navigating to the | |
235 // given entry. | |
236 bool InitRenderView(content::RenderViewHost* render_view_host, | |
237 const content::NavigationEntryImpl& entry); | |
238 | |
239 // Sets the pending RenderViewHost/WebUI to be the active one. Note that this | |
240 // doesn't require the pending render_view_host_ pointer to be non-NULL, since | |
241 // there could be Web UI switching as well. Call this for every commit. | |
242 void CommitPending(); | |
243 | |
244 // Helper method to terminate the pending RenderViewHost. | |
245 void CancelPending(); | |
246 | |
247 content::RenderViewHostImpl* UpdateRendererStateForNavigate( | |
248 const content::NavigationEntryImpl& entry); | |
249 | |
250 // Called when a renderer process is starting to close. We should not | |
251 // schedule new navigations in its swapped out RenderViewHosts after this. | |
252 void RendererProcessClosing(content::RenderProcessHost* render_process_host); | |
253 | |
254 // Our delegate, not owned by us. Guaranteed non-NULL. | |
255 Delegate* delegate_; | |
256 | |
257 // Whether a navigation requiring different RenderView's is pending. This is | |
258 // either cross-site request is (in the new process model), or when required | |
259 // for the view type (like view source versus not). | |
260 bool cross_navigation_pending_; | |
261 | |
262 // Implemented by the owner of this class, this delegate is installed into all | |
263 // the RenderViewHosts that we create. | |
264 content::RenderViewHostDelegate* render_view_delegate_; | |
265 | |
266 // Our RenderView host and its associated Web UI (if any, will be NULL for | |
267 // non-DOM-UI pages). This object is responsible for all communication with | |
268 // a child RenderView instance. | |
269 content::RenderViewHostImpl* render_view_host_; | |
270 scoped_ptr<WebUIImpl> web_ui_; | |
271 | |
272 // A RenderViewHost used to load a cross-site page. This remains hidden | |
273 // while a cross-site request is pending until it calls DidNavigate. It may | |
274 // have an associated Web UI, in which case the Web UI pointer will be non- | |
275 // NULL. | |
276 // | |
277 // The pending_web_ui may be non-NULL even when the pending_render_view_host_ | |
278 // is. This will happen when we're transitioning between two Web UI pages: | |
279 // the RVH won't be swapped, so the pending pointer will be unused, but there | |
280 // will be a pending Web UI associated with the navigation. | |
281 content::RenderViewHostImpl* pending_render_view_host_; | |
282 scoped_ptr<WebUIImpl> pending_web_ui_; | |
283 | |
284 // A map of site instance ID to swapped out RenderViewHosts. | |
285 typedef base::hash_map<int32, content::RenderViewHostImpl*> RenderViewHostMap; | |
286 RenderViewHostMap swapped_out_hosts_; | |
287 | |
288 // The intersitial page currently shown if any, not own by this class | |
289 // (the InterstitialPage is self-owned, it deletes itself when hidden). | |
290 InterstitialPageImpl* interstitial_page_; | |
291 | |
292 content::NotificationRegistrar registrar_; | |
293 | |
294 DISALLOW_COPY_AND_ASSIGN(RenderViewHostManager); | |
295 }; | |
296 | |
297 #endif // CONTENT_BROWSER_TAB_CONTENTS_RENDER_VIEW_HOST_MANAGER_H_ | |
OLD | NEW |