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"
20 struct FrameHostMsg_BeginNavigation_Params
;
24 class CrossProcessFrameConnector
;
25 class CrossSiteTransferringRequest
;
26 class InterstitialPageImpl
;
28 class NavigationControllerImpl
;
29 class NavigationEntry
;
30 class NavigationEntryImpl
;
31 class NavigationRequest
;
32 class RenderFrameHost
;
33 class RenderFrameHostDelegate
;
34 class RenderFrameHostImpl
;
35 class RenderFrameHostManagerTest
;
36 class RenderFrameProxyHost
;
38 class RenderViewHostImpl
;
39 class RenderWidgetHostDelegate
;
40 class RenderWidgetHostView
;
41 class TestWebContents
;
44 // Manages RenderFrameHosts for a FrameTreeNode. This class acts as a state
45 // machine to make cross-process navigations in a frame possible.
46 class CONTENT_EXPORT RenderFrameHostManager
: public NotificationObserver
{
48 // Functions implemented by our owner that we need.
50 // TODO(brettw) Clean this up! These are all the functions in WebContentsImpl
51 // that are required to run this class. The design should probably be better
52 // such that these are more clear.
54 // There is additional complexity that some of the functions we need in
55 // WebContentsImpl are inherited and non-virtual. These are named with
56 // "RenderManager" so that the duplicate implementation of them will be clear.
57 class CONTENT_EXPORT Delegate
{
59 // Initializes the given renderer if necessary and creates the view ID
60 // corresponding to this view host. If this method is not called and the
61 // process is not shared, then the WebContentsImpl will act as though the
62 // renderer is not running (i.e., it will render "sad tab"). This method is
63 // automatically called from LoadURL. |for_main_frame| indicates whether
64 // this RenderViewHost is used to render a top-level frame, so the
65 // appropriate RenderWidgetHostView type is used.
66 virtual bool CreateRenderViewForRenderManager(
67 RenderViewHost
* render_view_host
,
70 bool for_main_frame
) = 0;
71 virtual void BeforeUnloadFiredFromRenderManager(
72 bool proceed
, const base::TimeTicks
& proceed_time
,
73 bool* proceed_to_fire_unload
) = 0;
74 virtual void RenderProcessGoneFromRenderManager(
75 RenderViewHost
* render_view_host
) = 0;
76 virtual void UpdateRenderViewSizeForRenderManager() = 0;
77 virtual void CancelModalDialogsForRenderManager() = 0;
78 virtual void NotifySwappedFromRenderManager(RenderFrameHost
* old_host
,
79 RenderFrameHost
* new_host
,
80 bool is_main_frame
) = 0;
81 virtual NavigationControllerImpl
&
82 GetControllerForRenderManager() = 0;
84 // Create swapped out RenderViews in the given SiteInstance for each tab in
85 // the opener chain of this tab, if any. This allows the current tab to
86 // make cross-process script calls to its opener(s). Returns the route ID
87 // of the immediate opener, if one exists (otherwise MSG_ROUTING_NONE).
88 virtual int CreateOpenerRenderViewsForRenderManager(
89 SiteInstance
* instance
) = 0;
91 // Creates a WebUI object for the given URL if one applies. Ownership of the
92 // returned pointer will be passed to the caller. If no WebUI applies,
94 virtual WebUIImpl
* CreateWebUIForRenderManager(const GURL
& url
) = 0;
96 // Returns the navigation entry of the current navigation, or NULL if there
98 virtual NavigationEntry
*
99 GetLastCommittedNavigationEntryForRenderManager() = 0;
101 // Returns true if the location bar should be focused by default rather than
102 // the page contents. The view calls this function when the tab is focused
103 // to see what it should do.
104 virtual bool FocusLocationBarByDefault() = 0;
106 // Focuses the location bar.
107 virtual void SetFocusToLocationBar(bool select_all
) = 0;
109 // Creates a view and sets the size for the specified RVH.
110 virtual void CreateViewAndSetSizeForRVH(RenderViewHost
* rvh
) = 0;
112 // Returns true if views created for this delegate should be created in a
114 virtual bool IsHidden() = 0;
117 virtual ~Delegate() {}
120 // Used with FrameTree::ForEach to delete RenderFrameHosts pending shutdown
121 // from a FrameTreeNode's RenderFrameHostManager. Used during destruction of
123 static bool ClearRFHsPendingShutdown(FrameTreeNode
* node
);
125 // All three delegate pointers must be non-NULL and are not owned by this
126 // class. They must outlive this class. The RenderViewHostDelegate and
127 // RenderWidgetHostDelegate are what will be installed into all
128 // RenderViewHosts that are created.
130 // You must call Init() before using this class.
131 RenderFrameHostManager(
132 FrameTreeNode
* frame_tree_node
,
133 RenderFrameHostDelegate
* render_frame_delegate
,
134 RenderViewHostDelegate
* render_view_delegate
,
135 RenderWidgetHostDelegate
* render_widget_delegate
,
137 virtual ~RenderFrameHostManager();
139 // For arguments, see WebContentsImpl constructor.
140 void Init(BrowserContext
* browser_context
,
141 SiteInstance
* site_instance
,
143 int frame_routing_id
);
145 // Returns the currently active RenderFrameHost.
147 // This will be non-NULL between Init() and Shutdown(). You may want to NULL
148 // check it in many cases, however. Windows can send us messages during the
149 // destruction process after it has been shut down.
150 RenderFrameHostImpl
* current_frame_host() const {
151 return render_frame_host_
.get();
154 // TODO(creis): Remove this when we no longer use RVH for navigation.
155 RenderViewHostImpl
* current_host() const;
157 // Returns the view associated with the current RenderViewHost, or NULL if
158 // there is no current one.
159 RenderWidgetHostView
* GetRenderWidgetHostView() const;
161 RenderFrameProxyHost
* GetProxyToParent();
163 // Returns the pending RenderFrameHost, or NULL if there is no pending one.
164 RenderFrameHostImpl
* pending_frame_host() const {
165 return pending_render_frame_host_
.get();
168 // TODO(creis): Remove this when we no longer use RVH for navigation.
169 RenderViewHostImpl
* pending_render_view_host() const;
171 // Returns the current committed Web UI or NULL if none applies.
172 WebUIImpl
* web_ui() const { return web_ui_
.get(); }
174 // Returns the Web UI for the pending navigation, or NULL of none applies.
175 WebUIImpl
* pending_web_ui() const {
176 return pending_web_ui_
.get() ? pending_web_ui_
.get() :
177 pending_and_current_web_ui_
.get();
180 // Sets the pending Web UI for the pending navigation, ensuring that the
181 // bindings are appropriate for the given NavigationEntry.
182 void SetPendingWebUI(const NavigationEntryImpl
& entry
);
184 // Called when we want to instruct the renderer to navigate to the given
185 // navigation entry. It may create a new RenderFrameHost or re-use an existing
186 // one. The RenderFrameHost to navigate will be returned. Returns NULL if one
187 // could not be created.
188 RenderFrameHostImpl
* Navigate(const NavigationEntryImpl
& entry
);
190 // Instructs the various live views to stop. Called when the user directed the
191 // page to stop loading.
194 // Notifies the regular and pending RenderViewHosts that a load is or is not
195 // happening. Even though the message is only for one of them, we don't know
196 // which one so we tell both.
197 void SetIsLoading(bool is_loading
);
199 // Whether to close the tab or not when there is a hang during an unload
200 // handler. If we are mid-crosssite navigation, then we should proceed
201 // with the navigation instead of closing the tab.
202 bool ShouldCloseTabOnUnresponsiveRenderer();
204 // Confirms whether we should close the page or navigate away. This is called
205 // before a cross-site request or before a tab/window is closed (as indicated
206 // by the first parameter) to allow the appropriate renderer to approve or
207 // deny the request. |proceed| indicates whether the user chose to proceed.
208 // |proceed_time| is the time when the request was allowed to proceed.
209 void OnBeforeUnloadACK(bool for_cross_site_transition
,
211 const base::TimeTicks
& proceed_time
);
213 // The |pending_render_frame_host| is ready to commit a page. We should
214 // ensure that the old RenderFrameHost runs its unload handler first and
215 // determine whether a RenderFrameHost transfer is needed.
216 // |cross_site_transferring_request| is NULL if a request is not being
217 // transferred between renderers.
218 void OnCrossSiteResponse(
219 RenderFrameHostImpl
* pending_render_frame_host
,
220 const GlobalRequestID
& global_request_id
,
221 scoped_ptr
<CrossSiteTransferringRequest
> cross_site_transferring_request
,
222 const std::vector
<GURL
>& transfer_url_chain
,
223 const Referrer
& referrer
,
224 PageTransition page_transition
,
225 bool should_replace_current_entry
);
227 // Received a response from CrossSiteResourceHandler. If the navigation
228 // specifies a transition, this is called and the navigation will not resume
229 // until ResumeResponseDeferredAtStart.
230 void OnDeferredAfterResponseStarted(
231 const GlobalRequestID
& global_request_id
,
232 RenderFrameHostImpl
* pending_render_frame_host
);
234 // Resume navigation paused after receiving response headers.
235 void ResumeResponseDeferredAtStart();
237 // The RenderFrameHost has been swapped out, so we should resume the pending
238 // network response and allow the pending RenderFrameHost to commit.
239 void SwappedOut(RenderFrameHostImpl
* render_frame_host
);
241 // Called when a renderer's frame navigates.
242 void DidNavigateFrame(RenderFrameHostImpl
* render_frame_host
);
244 // Called when a renderer sets its opener to null.
245 void DidDisownOpener(RenderViewHost
* render_view_host
);
247 // Helper method to create and initialize a RenderFrameHost. If |swapped_out|
248 // is true, it will be initially placed on the swapped out hosts list.
249 // Otherwise, it will be used for a pending cross-site navigation.
250 // Returns the routing id of the *view* associated with the frame.
251 int CreateRenderFrame(SiteInstance
* instance
,
256 // Sets the passed passed interstitial as the currently showing interstitial.
257 // |interstitial_page| should be non NULL (use the remove_interstitial_page
258 // method to unset the interstitial) and no interstitial page should be set
259 // when there is already a non NULL interstitial page set.
260 void set_interstitial_page(InterstitialPageImpl
* interstitial_page
) {
261 DCHECK(!interstitial_page_
&& interstitial_page
);
262 interstitial_page_
= interstitial_page
;
265 // Unsets the currently showing interstitial.
266 void remove_interstitial_page() {
267 DCHECK(interstitial_page_
);
268 interstitial_page_
= NULL
;
271 // Returns the currently showing interstitial, NULL if no interstitial is
273 InterstitialPageImpl
* interstitial_page() const { return interstitial_page_
; }
275 // NotificationObserver implementation.
276 virtual void Observe(int type
,
277 const NotificationSource
& source
,
278 const NotificationDetails
& details
) OVERRIDE
;
280 // Returns whether the given RenderFrameHost (or its associated
281 // RenderViewHost) is on the list of swapped out RenderFrameHosts.
282 bool IsRVHOnSwappedOutList(RenderViewHostImpl
* rvh
) const;
283 bool IsOnSwappedOutList(RenderFrameHostImpl
* rfh
) const;
285 // Returns the swapped out RenderViewHost or RenderFrameHost for the given
286 // SiteInstance, if any. This method is *deprecated* and
287 // GetRenderFrameProxyHost should be used.
288 RenderViewHostImpl
* GetSwappedOutRenderViewHost(SiteInstance
* instance
) const;
289 RenderFrameProxyHost
* GetRenderFrameProxyHost(
290 SiteInstance
* instance
) const;
292 // Runs the unload handler in the current page, when we know that a pending
293 // cross-process navigation is going to commit. We may initiate a transfer
294 // to a new process after this completes or times out.
295 void SwapOutOldPage();
297 // Deletes a RenderFrameHost that was pending shutdown.
298 void ClearPendingShutdownRFHForSiteInstance(int32 site_instance_id
,
299 RenderFrameHostImpl
* rfh
);
301 // Deletes any proxy hosts associated with this node. Used during destruction
302 // of WebContentsImpl.
303 void ResetProxyHosts();
305 // Used to start a navigation, part of browser-side navigation project.
306 void OnBeginNavigation(const FrameHostMsg_BeginNavigation_Params
& params
);
309 friend class RenderFrameHostManagerTest
;
310 friend class TestWebContents
;
312 // Tracks information about a navigation while a cross-process transition is
313 // in progress, in case we need to transfer it to a new RenderFrameHost.
314 // When a request is being transferred, deleting the PendingNavigationParams,
315 // and thus |cross_site_transferring_request|, will cancel the request being
316 // transferred, unless its ReleaseRequest method has been called.
317 struct PendingNavigationParams
{
318 PendingNavigationParams(
319 const GlobalRequestID
& global_request_id
,
320 scoped_ptr
<CrossSiteTransferringRequest
>
321 cross_site_transferring_request
,
322 const std::vector
<GURL
>& transfer_url
,
324 PageTransition page_transition
,
326 bool should_replace_current_entry
);
327 ~PendingNavigationParams();
329 // The child ID and request ID for the pending navigation. Present whether
330 // |request_transfer| is NULL or not.
331 GlobalRequestID global_request_id
;
333 // If a pending request needs to be transferred to another process, this
334 // owns the request until it's transferred to the new process, so it will be
335 // cleaned up if the navigation is cancelled. Otherwise, this is NULL.
336 scoped_ptr
<CrossSiteTransferringRequest
> cross_site_transferring_request
;
338 // If |request_transfer| is non-NULL, the values below are all set.
340 // The first entry is the original request URL, and the last entry is the
341 // destination URL to request in the new process.
342 std::vector
<GURL
> transfer_url_chain
;
344 // This is the referrer to use for the request in the new process.
347 // This is the transition type for the original navigation.
348 PageTransition page_transition
;
350 // This is the frame routing ID to use in RequestTransferURL.
353 // This is whether the navigation should replace the current history entry.
354 bool should_replace_current_entry
;
357 // Returns the current navigation request (used in the PlzNavigate navigation
358 // logic refactoring project).
359 NavigationRequest
* navigation_request_for_testing() const {
360 return navigation_request_
.get(); }
362 // Used with FrameTree::ForEach to erase RenderFrameProxyHosts from a
363 // FrameTreeNode's RenderFrameHostManager.
364 static bool ClearProxiesInSiteInstance(int32 site_instance_id
,
365 FrameTreeNode
* node
);
367 // Returns whether this tab should transition to a new renderer for
368 // cross-site URLs. Enabled unless we see the --process-per-tab command line
369 // switch. Can be overridden in unit tests.
370 bool ShouldTransitionCrossSite();
372 // Returns true if for the navigation from |current_entry| to |new_entry|,
373 // a new SiteInstance and BrowsingInstance should be created (even if we are
374 // in a process model that doesn't usually swap). This forces a process swap
375 // and severs script connections with existing tabs. Cases where this can
376 // happen include transitions between WebUI and regular web pages.
377 // Either of the entries may be NULL.
378 bool ShouldSwapBrowsingInstancesForNavigation(
379 const NavigationEntry
* current_entry
,
380 const NavigationEntryImpl
* new_entry
) const;
382 // Returns true if it is safe to reuse the current WebUI when navigating from
383 // |current_entry| to |new_entry|.
384 bool ShouldReuseWebUI(
385 const NavigationEntry
* current_entry
,
386 const NavigationEntryImpl
* new_entry
) const;
388 // Returns an appropriate SiteInstance object for the given NavigationEntry,
389 // possibly reusing the current SiteInstance. If --process-per-tab is used,
390 // this is only called when ShouldSwapBrowsingInstancesForNavigation returns
392 SiteInstance
* GetSiteInstanceForEntry(
393 const NavigationEntryImpl
& entry
,
394 SiteInstance
* current_instance
,
395 bool force_browsing_instance_swap
);
397 // Creates a RenderFrameHost and corresponding RenderViewHost if necessary.
398 scoped_ptr
<RenderFrameHostImpl
> CreateRenderFrameHost(SiteInstance
* instance
,
400 int frame_routing_id
,
404 // Sets up the necessary state for a new RenderViewHost with the given opener,
405 // if necessary. It creates a RenderFrameProxy in the target renderer process
406 // with the given |proxy_routing_id|, which is used to route IPC messages when
407 // in swapped out state. Returns early if the RenderViewHost has already been
408 // initialized for another RenderFrameHost.
409 // TODO(creis): opener_route_id is currently for the RenderViewHost but should
410 // be for the RenderFrame, since frames can have openers.
411 bool InitRenderView(RenderViewHost
* render_view_host
,
413 int proxy_routing_id
,
414 bool for_main_frame
);
416 // Sets the pending RenderFrameHost/WebUI to be the active one. Note that this
417 // doesn't require the pending render_frame_host_ pointer to be non-NULL,
418 // since there could be Web UI switching as well. Call this for every commit.
419 void CommitPending();
421 // Shutdown all RenderFrameHosts in a SiteInstance. This is called to shutdown
422 // frames when all the frames in a SiteInstance are confirmed to be swapped
424 void ShutdownRenderFrameHostsInSiteInstance(int32 site_instance_id
);
426 // Helper method to terminate the pending RenderViewHost.
427 void CancelPending();
429 // Helper method to set the active RenderFrameHost. Returns the old
430 // RenderFrameHost and updates counts.
431 scoped_ptr
<RenderFrameHostImpl
> SetRenderFrameHost(
432 scoped_ptr
<RenderFrameHostImpl
> render_frame_host
);
434 RenderFrameHostImpl
* UpdateStateForNavigate(
435 const NavigationEntryImpl
& entry
);
437 // Called when a renderer process is starting to close. We should not
438 // schedule new navigations in its swapped out RenderFrameHosts after this.
439 void RendererProcessClosing(RenderProcessHost
* render_process_host
);
441 // For use in creating RenderFrameHosts.
442 FrameTreeNode
* frame_tree_node_
;
444 // Our delegate, not owned by us. Guaranteed non-NULL.
447 // Whether a navigation requiring different RenderFrameHosts is pending. This
448 // is either for cross-site requests or when required for the process type
450 bool cross_navigation_pending_
;
452 // Implemented by the owner of this class. These delegates are installed into
453 // all the RenderFrameHosts that we create.
454 RenderFrameHostDelegate
* render_frame_delegate_
;
455 RenderViewHostDelegate
* render_view_delegate_
;
456 RenderWidgetHostDelegate
* render_widget_delegate_
;
458 // Our RenderFrameHost and its associated Web UI (if any, will be NULL for
459 // non-WebUI pages). This object is responsible for all communication with
460 // a child RenderFrame instance.
461 // For now, RenderFrameHost keeps a RenderViewHost in its SiteInstance alive.
462 // Eventually, RenderViewHost will be replaced with a page context.
463 scoped_ptr
<RenderFrameHostImpl
> render_frame_host_
;
464 scoped_ptr
<WebUIImpl
> web_ui_
;
466 // A RenderFrameHost used to load a cross-site page. This remains hidden
467 // while a cross-site request is pending until it calls DidNavigate. It may
468 // have an associated Web UI, in which case the Web UI pointer will be non-
471 // The |pending_web_ui_| may be non-NULL even when the
472 // |pending_render_frame_host_| is NULL. This will happen when we're
473 // transitioning between two Web UI pages: the RFH won't be swapped, so the
474 // pending pointer will be unused, but there will be a pending Web UI
475 // associated with the navigation.
476 scoped_ptr
<RenderFrameHostImpl
> pending_render_frame_host_
;
478 // Tracks information about any current pending cross-process navigation.
479 scoped_ptr
<PendingNavigationParams
> pending_nav_params_
;
481 // Tracks information about any navigation paused after receiving response
483 scoped_ptr
<GlobalRequestID
> response_started_id_
;
485 // If either of these is non-NULL, the pending navigation is to a chrome:
486 // page. The scoped_ptr is used if pending_web_ui_ != web_ui_, the WeakPtr is
487 // used for when they reference the same object. If either is non-NULL, the
488 // other should be NULL.
489 scoped_ptr
<WebUIImpl
> pending_web_ui_
;
490 base::WeakPtr
<WebUIImpl
> pending_and_current_web_ui_
;
492 // A map of site instance ID to RenderFrameProxyHosts.
493 typedef base::hash_map
<int32
, RenderFrameProxyHost
*> RenderFrameProxyHostMap
;
494 RenderFrameProxyHostMap proxy_hosts_
;
496 // A map of RenderFrameHosts pending shutdown.
497 typedef base::hash_map
<int32
, linked_ptr
<RenderFrameHostImpl
> >
499 RFHPendingDeleteMap pending_delete_hosts_
;
501 // The intersitial page currently shown if any, not own by this class
502 // (the InterstitialPage is self-owned, it deletes itself when hidden).
503 InterstitialPageImpl
* interstitial_page_
;
505 NotificationRegistrar registrar_
;
507 // Owns a navigation request that originated in that frame until it commits.
508 scoped_ptr
<NavigationRequest
> navigation_request_
;
510 base::WeakPtrFactory
<RenderFrameHostManager
> weak_factory_
;
512 DISALLOW_COPY_AND_ASSIGN(RenderFrameHostManager
);
515 } // namespace content
517 #endif // CONTENT_BROWSER_FRAME_HOST_RENDER_FRAME_HOST_MANAGER_H_