1 // Copyright 2013 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 CONTENT_BROWSER_FRAME_HOST_RENDER_FRAME_HOST_MANAGER_H_
6 #define CONTENT_BROWSER_FRAME_HOST_RENDER_FRAME_HOST_MANAGER_H_
8 #include "base/basictypes.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "content/browser/renderer_host/render_view_host_delegate.h"
13 #include "content/browser/site_instance_impl.h"
14 #include "content/common/content_export.h"
15 #include "content/public/browser/global_request_id.h"
16 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h"
18 #include "content/public/common/referrer.h"
23 class CrossProcessFrameConnector
;
24 class CrossSiteTransferringRequest
;
25 class InterstitialPageImpl
;
27 class NavigationControllerImpl
;
28 class NavigationEntry
;
29 class NavigationEntryImpl
;
30 class RenderFrameHostDelegate
;
31 class RenderFrameHostImpl
;
32 class RenderFrameHostManagerTest
;
34 class RenderViewHostImpl
;
35 class RenderWidgetHostDelegate
;
36 class RenderWidgetHostView
;
37 class TestWebContents
;
40 // Manages RenderFrameHosts for a FrameTreeNode. This class acts as a state
41 // machine to make cross-process navigations in a frame possible.
42 class CONTENT_EXPORT RenderFrameHostManager
: public NotificationObserver
{
44 // Functions implemented by our owner that we need.
46 // TODO(brettw) Clean this up! These are all the functions in WebContentsImpl
47 // that are required to run this class. The design should probably be better
48 // such that these are more clear.
50 // There is additional complexity that some of the functions we need in
51 // WebContentsImpl are inherited and non-virtual. These are named with
52 // "RenderManager" so that the duplicate implementation of them will be clear.
53 class CONTENT_EXPORT Delegate
{
55 // Initializes the given renderer if necessary and creates the view ID
56 // corresponding to this view host. If this method is not called and the
57 // process is not shared, then the WebContentsImpl will act as though the
58 // renderer is not running (i.e., it will render "sad tab"). This method is
59 // automatically called from LoadURL.
61 // If you are attaching to an already-existing RenderView, you should call
62 // InitWithExistingID.
63 virtual bool CreateRenderViewForRenderManager(
64 RenderViewHost
* render_view_host
,
66 CrossProcessFrameConnector
* cross_process_frame_connector
) = 0;
67 virtual void BeforeUnloadFiredFromRenderManager(
68 bool proceed
, const base::TimeTicks
& proceed_time
,
69 bool* proceed_to_fire_unload
) = 0;
70 virtual void RenderProcessGoneFromRenderManager(
71 RenderViewHost
* render_view_host
) = 0;
72 virtual void UpdateRenderViewSizeForRenderManager() = 0;
73 virtual void CancelModalDialogsForRenderManager() = 0;
74 virtual void NotifySwappedFromRenderManager(
75 RenderViewHost
* old_host
, RenderViewHost
* new_host
) = 0;
76 virtual NavigationControllerImpl
&
77 GetControllerForRenderManager() = 0;
79 // Create swapped out RenderViews in the given SiteInstance for each tab in
80 // the opener chain of this tab, if any. This allows the current tab to
81 // make cross-process script calls to its opener(s). Returns the route ID
82 // of the immediate opener, if one exists (otherwise MSG_ROUTING_NONE).
83 virtual int CreateOpenerRenderViewsForRenderManager(
84 SiteInstance
* instance
) = 0;
86 // Creates a WebUI object for the given URL if one applies. Ownership of the
87 // returned pointer will be passed to the caller. If no WebUI applies,
89 virtual WebUIImpl
* CreateWebUIForRenderManager(const GURL
& url
) = 0;
91 // Returns the navigation entry of the current navigation, or NULL if there
93 virtual NavigationEntry
*
94 GetLastCommittedNavigationEntryForRenderManager() = 0;
96 // Returns true if the location bar should be focused by default rather than
97 // the page contents. The view calls this function when the tab is focused
98 // to see what it should do.
99 virtual bool FocusLocationBarByDefault() = 0;
101 // Focuses the location bar.
102 virtual void SetFocusToLocationBar(bool select_all
) = 0;
104 // Creates a view and sets the size for the specified RVH.
105 virtual void CreateViewAndSetSizeForRVH(RenderViewHost
* rvh
) = 0;
107 // Returns true if views created for this delegate should be created in a
109 virtual bool IsHidden() = 0;
112 virtual ~Delegate() {}
115 // Used with FrameTree::ForEach to delete RenderFrameHosts pending shutdown
116 // from a FrameTreeNode's RenderFrameHostManager. Used during destruction of
118 static bool ClearRFHsPendingShutdown(FrameTreeNode
* node
);
120 // All three delegate pointers must be non-NULL and are not owned by this
121 // class. They must outlive this class. The RenderViewHostDelegate and
122 // RenderWidgetHostDelegate are what will be installed into all
123 // RenderViewHosts that are created.
125 // You must call Init() before using this class.
126 RenderFrameHostManager(
127 FrameTreeNode
* frame_tree_node
,
128 RenderFrameHostDelegate
* render_frame_delegate
,
129 RenderViewHostDelegate
* render_view_delegate
,
130 RenderWidgetHostDelegate
* render_widget_delegate
,
132 virtual ~RenderFrameHostManager();
134 // For arguments, see WebContentsImpl constructor.
135 void Init(BrowserContext
* browser_context
,
136 SiteInstance
* site_instance
,
138 int frame_routing_id
);
140 // Returns the currently active RenderFrameHost.
142 // This will be non-NULL between Init() and Shutdown(). You may want to NULL
143 // check it in many cases, however. Windows can send us messages during the
144 // destruction process after it has been shut down.
145 RenderFrameHostImpl
* current_frame_host() const {
146 return render_frame_host_
.get();
149 // TODO(creis): Remove this when we no longer use RVH for navigation.
150 RenderViewHostImpl
* current_host() const;
152 // Returns the view associated with the current RenderViewHost, or NULL if
153 // there is no current one.
154 RenderWidgetHostView
* GetRenderWidgetHostView() const;
156 // Returns the pending RenderFrameHost, or NULL if there is no pending one.
157 RenderFrameHostImpl
* pending_frame_host() const {
158 return pending_render_frame_host_
.get();
161 // TODO(creis): Remove this when we no longer use RVH for navigation.
162 RenderViewHostImpl
* pending_render_view_host() const;
164 // Returns the current committed Web UI or NULL if none applies.
165 WebUIImpl
* web_ui() const { return web_ui_
.get(); }
167 // Returns the Web UI for the pending navigation, or NULL of none applies.
168 WebUIImpl
* pending_web_ui() const {
169 return pending_web_ui_
.get() ? pending_web_ui_
.get() :
170 pending_and_current_web_ui_
.get();
173 // Sets the pending Web UI for the pending navigation, ensuring that the
174 // bindings are appropriate for the given NavigationEntry.
175 void SetPendingWebUI(const NavigationEntryImpl
& entry
);
177 // Called when we want to instruct the renderer to navigate to the given
178 // navigation entry. It may create a new RenderFrameHost or re-use an existing
179 // one. The RenderFrameHost to navigate will be returned. Returns NULL if one
180 // could not be created.
181 RenderFrameHostImpl
* Navigate(const NavigationEntryImpl
& entry
);
183 // Instructs the various live views to stop. Called when the user directed the
184 // page to stop loading.
187 // Notifies the regular and pending RenderViewHosts that a load is or is not
188 // happening. Even though the message is only for one of them, we don't know
189 // which one so we tell both.
190 void SetIsLoading(bool is_loading
);
192 // Whether to close the tab or not when there is a hang during an unload
193 // handler. If we are mid-crosssite navigation, then we should proceed
194 // with the navigation instead of closing the tab.
195 bool ShouldCloseTabOnUnresponsiveRenderer();
197 // Confirms whether we should close the page or navigate away. This is called
198 // before a cross-site request or before a tab/window is closed (as indicated
199 // by the first parameter) to allow the appropriate renderer to approve or
200 // deny the request. |proceed| indicates whether the user chose to proceed.
201 // |proceed_time| is the time when the request was allowed to proceed.
202 void OnBeforeUnloadACK(bool for_cross_site_transition
,
204 const base::TimeTicks
& proceed_time
);
206 // The |pending_render_frame_host| is ready to commit a page. We should
207 // ensure that the old RenderFrameHost runs its unload handler first and
208 // determine whether a RenderFrameHost transfer is needed.
209 // |cross_site_transferring_request| is NULL if a request is not being
210 // transferred between renderers.
211 void OnCrossSiteResponse(
212 RenderFrameHostImpl
* pending_render_frame_host
,
213 const GlobalRequestID
& global_request_id
,
214 scoped_ptr
<CrossSiteTransferringRequest
> cross_site_transferring_request
,
215 const std::vector
<GURL
>& transfer_url_chain
,
216 const Referrer
& referrer
,
217 PageTransition page_transition
,
218 bool should_replace_current_entry
);
220 // The RenderFrameHost has been swapped out, so we should resume the pending
221 // network response and allow the pending RenderFrameHost to commit.
222 void SwappedOut(RenderFrameHostImpl
* render_frame_host
);
224 // Called when a renderer's frame navigates.
225 void DidNavigateFrame(RenderFrameHostImpl
* render_frame_host
);
227 // Called when a renderer sets its opener to null.
228 void DidDisownOpener(RenderViewHost
* render_view_host
);
230 // Helper method to create and initialize a RenderFrameHost. If |swapped_out|
231 // is true, it will be initially placed on the swapped out hosts list.
232 // Otherwise, it will be used for a pending cross-site navigation.
233 int CreateRenderFrame(SiteInstance
* instance
,
238 // Sets the passed passed interstitial as the currently showing interstitial.
239 // |interstitial_page| should be non NULL (use the remove_interstitial_page
240 // method to unset the interstitial) and no interstitial page should be set
241 // when there is already a non NULL interstitial page set.
242 void set_interstitial_page(InterstitialPageImpl
* interstitial_page
) {
243 DCHECK(!interstitial_page_
&& interstitial_page
);
244 interstitial_page_
= interstitial_page
;
247 // Unsets the currently showing interstitial.
248 void remove_interstitial_page() {
249 DCHECK(interstitial_page_
);
250 interstitial_page_
= NULL
;
253 // Returns the currently showing interstitial, NULL if no interstitial is
255 InterstitialPageImpl
* interstitial_page() const { return interstitial_page_
; }
257 // NotificationObserver implementation.
258 virtual void Observe(int type
,
259 const NotificationSource
& source
,
260 const NotificationDetails
& details
) OVERRIDE
;
262 // Called when a RenderViewHost is about to be deleted.
263 void RenderViewDeleted(RenderViewHost
* rvh
);
265 // Returns whether the given RenderFrameHost (or its associated
266 // RenderViewHost) is on the list of swapped out RenderFrameHosts.
267 bool IsRVHOnSwappedOutList(RenderViewHostImpl
* rvh
) const;
268 bool IsOnSwappedOutList(RenderFrameHostImpl
* rfh
) const;
270 // Returns the swapped out RenderViewHost or RenderFrameHost for the given
271 // SiteInstance, if any.
272 RenderViewHostImpl
* GetSwappedOutRenderViewHost(SiteInstance
* instance
) const;
273 RenderFrameHostImpl
* GetSwappedOutRenderFrameHost(
274 SiteInstance
* instance
) const;
276 // Runs the unload handler in the current page, when we know that a pending
277 // cross-process navigation is going to commit. We may initiate a transfer
278 // to a new process after this completes or times out.
279 void SwapOutOldPage();
281 // Deletes a RenderFrameHost that was pending shutdown.
282 void ClearPendingShutdownRFHForSiteInstance(int32 site_instance_id
,
283 RenderFrameHostImpl
* rfh
);
286 friend class RenderFrameHostManagerTest
;
287 friend class TestWebContents
;
289 // Tracks information about a navigation while a cross-process transition is
290 // in progress, in case we need to transfer it to a new RenderFrameHost.
291 // When a request is being transferred, deleting the PendingNavigationParams,
292 // and thus |cross_site_transferring_request|, will cancel the request being
293 // transferred, unless its ReleaseRequest method has been called.
294 struct PendingNavigationParams
{
295 PendingNavigationParams(
296 const GlobalRequestID
& global_request_id
,
297 scoped_ptr
<CrossSiteTransferringRequest
>
298 cross_site_transferring_request
,
299 const std::vector
<GURL
>& transfer_url
,
301 PageTransition page_transition
,
303 bool should_replace_current_entry
);
304 ~PendingNavigationParams();
306 // The child ID and request ID for the pending navigation. Present whether
307 // |request_transfer| is NULL or not.
308 GlobalRequestID global_request_id
;
310 // If a pending request needs to be transferred to another process, this
311 // owns the request until it's transferred to the new process, so it will be
312 // cleaned up if the navigation is cancelled. Otherwise, this is NULL.
313 scoped_ptr
<CrossSiteTransferringRequest
> cross_site_transferring_request
;
315 // If |request_transfer| is non-NULL, the values below are all set.
317 // The first entry is the original request URL, and the last entry is the
318 // destination URL to request in the new process.
319 std::vector
<GURL
> transfer_url_chain
;
321 // This is the referrer to use for the request in the new process.
324 // This is the transition type for the original navigation.
325 PageTransition page_transition
;
327 // This is the frame routing ID to use in RequestTransferURL.
330 // This is whether the navigation should replace the current history entry.
331 bool should_replace_current_entry
;
334 // Used with FrameTree::ForEach to erase inactive RenderFrameHosts from a
335 // FrameTreeNode's RenderFrameHostManager.
336 static bool ClearSwappedOutRFHsInSiteInstance(int32 site_instance_id
,
337 FrameTreeNode
* node
);
339 // Returns whether this tab should transition to a new renderer for
340 // cross-site URLs. Enabled unless we see the --process-per-tab command line
341 // switch. Can be overridden in unit tests.
342 bool ShouldTransitionCrossSite();
344 // Returns true if for the navigation from |current_entry| to |new_entry|,
345 // a new SiteInstance and BrowsingInstance should be created (even if we are
346 // in a process model that doesn't usually swap). This forces a process swap
347 // and severs script connections with existing tabs. Cases where this can
348 // happen include transitions between WebUI and regular web pages.
349 // Either of the entries may be NULL.
350 bool ShouldSwapBrowsingInstancesForNavigation(
351 const NavigationEntry
* current_entry
,
352 const NavigationEntryImpl
* new_entry
) const;
354 // Returns true if it is safe to reuse the current WebUI when navigating from
355 // |current_entry| to |new_entry|.
356 bool ShouldReuseWebUI(
357 const NavigationEntry
* current_entry
,
358 const NavigationEntryImpl
* new_entry
) const;
360 // Returns an appropriate SiteInstance object for the given NavigationEntry,
361 // possibly reusing the current SiteInstance. If --process-per-tab is used,
362 // this is only called when ShouldSwapBrowsingInstancesForNavigation returns
364 SiteInstance
* GetSiteInstanceForEntry(
365 const NavigationEntryImpl
& entry
,
366 SiteInstance
* current_instance
,
367 bool force_browsing_instance_swap
);
369 // Creates a RenderFrameHost and corresponding RenderViewHost if necessary.
370 RenderFrameHostImpl
* CreateRenderFrameHost(SiteInstance
* instance
,
372 int frame_routing_id
,
376 // Sets up the necessary state for a new RenderViewHost with the given opener,
377 // if necessary. Returns early if the RenderViewHost has already been
378 // initialized for another RenderFrameHost.
379 // TODO(creis): opener_route_id is currently for the RenderViewHost but should
380 // be for the RenderFrame, since frames can have openers.
381 bool InitRenderView(RenderViewHost
* render_view_host
, int opener_route_id
);
383 // Sets the pending RenderFrameHost/WebUI to be the active one. Note that this
384 // doesn't require the pending render_frame_host_ pointer to be non-NULL,
385 // since there could be Web UI switching as well. Call this for every commit.
386 void CommitPending();
388 // Shutdown all RenderFrameHosts in a SiteInstance. This is called to shutdown
389 // frames when all the frames in a SiteInstance are confirmed to be swapped
391 void ShutdownRenderFrameHostsInSiteInstance(int32 site_instance_id
);
393 // Helper method to terminate the pending RenderViewHost.
394 void CancelPending();
396 RenderFrameHostImpl
* UpdateRendererStateForNavigate(
397 const NavigationEntryImpl
& entry
);
399 // Called when a renderer process is starting to close. We should not
400 // schedule new navigations in its swapped out RenderFrameHosts after this.
401 void RendererProcessClosing(RenderProcessHost
* render_process_host
);
403 // For use in creating RenderFrameHosts.
404 FrameTreeNode
* frame_tree_node_
;
406 // Our delegate, not owned by us. Guaranteed non-NULL.
409 // Whether a navigation requiring different RenderFrameHosts is pending. This
410 // is either for cross-site requests or when required for the process type
412 bool cross_navigation_pending_
;
414 // Implemented by the owner of this class. These delegates are installed into
415 // all the RenderFrameHosts that we create.
416 RenderFrameHostDelegate
* render_frame_delegate_
;
417 RenderViewHostDelegate
* render_view_delegate_
;
418 RenderWidgetHostDelegate
* render_widget_delegate_
;
420 // Our RenderFrameHost and its associated Web UI (if any, will be NULL for
421 // non-WebUI pages). This object is responsible for all communication with
422 // a child RenderFrame instance.
423 // For now, RenderFrameHost keeps a RenderViewHost in its SiteInstance alive.
424 // Eventually, RenderViewHost will be replaced with a page context.
425 scoped_ptr
<RenderFrameHostImpl
> render_frame_host_
;
426 scoped_ptr
<WebUIImpl
> web_ui_
;
428 // A RenderFrameHost used to load a cross-site page. This remains hidden
429 // while a cross-site request is pending until it calls DidNavigate. It may
430 // have an associated Web UI, in which case the Web UI pointer will be non-
433 // The |pending_web_ui_| may be non-NULL even when the
434 // |pending_render_frame_host_| is NULL. This will happen when we're
435 // transitioning between two Web UI pages: the RFH won't be swapped, so the
436 // pending pointer will be unused, but there will be a pending Web UI
437 // associated with the navigation.
438 scoped_ptr
<RenderFrameHostImpl
> pending_render_frame_host_
;
440 // Tracks information about any current pending cross-process navigation.
441 scoped_ptr
<PendingNavigationParams
> pending_nav_params_
;
443 // If either of these is non-NULL, the pending navigation is to a chrome:
444 // page. The scoped_ptr is used if pending_web_ui_ != web_ui_, the WeakPtr is
445 // used for when they reference the same object. If either is non-NULL, the
446 // other should be NULL.
447 scoped_ptr
<WebUIImpl
> pending_web_ui_
;
448 base::WeakPtr
<WebUIImpl
> pending_and_current_web_ui_
;
450 // A map of site instance ID to swapped out RenderFrameHosts. This may
451 // include pending_render_frame_host_ for navigations to existing entries.
452 typedef base::hash_map
<int32
, RenderFrameHostImpl
*> RenderFrameHostMap
;
453 RenderFrameHostMap swapped_out_hosts_
;
455 // A map of RenderFrameHosts pending shutdown.
456 typedef base::hash_map
<int32
, linked_ptr
<RenderFrameHostImpl
> >
458 RFHPendingDeleteMap pending_delete_hosts_
;
460 // The intersitial page currently shown if any, not own by this class
461 // (the InterstitialPage is self-owned, it deletes itself when hidden).
462 InterstitialPageImpl
* interstitial_page_
;
464 NotificationRegistrar registrar_
;
466 // When |render_frame_host_| is in a different process from its parent in
467 // the frame tree, this class connects its associated RenderWidgetHostView
468 // to the proxy RenderFrameHost for the parent's renderer process. NULL
469 // when |render_frame_host_| is the frame tree root or is in the same
470 // process as its parent.
471 CrossProcessFrameConnector
* cross_process_frame_connector_
;
473 base::WeakPtrFactory
<RenderFrameHostManager
> weak_factory_
;
475 DISALLOW_COPY_AND_ASSIGN(RenderFrameHostManager
);
478 } // namespace content
480 #endif // CONTENT_BROWSER_FRAME_HOST_RENDER_FRAME_HOST_MANAGER_H_