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;
33 class RenderFrameProxyHost;
35 class RenderViewHostImpl;
36 class RenderWidgetHostDelegate;
37 class RenderWidgetHostView;
38 class TestWebContents;
41 // Manages RenderFrameHosts for a FrameTreeNode. This class acts as a state
42 // machine to make cross-process navigations in a frame possible.
43 class CONTENT_EXPORT RenderFrameHostManager : public NotificationObserver {
45 // Functions implemented by our owner that we need.
47 // TODO(brettw) Clean this up! These are all the functions in WebContentsImpl
48 // that are required to run this class. The design should probably be better
49 // such that these are more clear.
51 // There is additional complexity that some of the functions we need in
52 // WebContentsImpl are inherited and non-virtual. These are named with
53 // "RenderManager" so that the duplicate implementation of them will be clear.
54 class CONTENT_EXPORT Delegate {
56 // Initializes the given renderer if necessary and creates the view ID
57 // corresponding to this view host. If this method is not called and the
58 // process is not shared, then the WebContentsImpl will act as though the
59 // renderer is not running (i.e., it will render "sad tab"). This method is
60 // automatically called from LoadURL. |for_main_frame| indicates whether
61 // this RenderViewHost is used to render a top-level frame, so the
62 // appropriate RenderWidgetHostView type is used.
63 virtual bool CreateRenderViewForRenderManager(
64 RenderViewHost* render_view_host,
67 bool for_main_frame) = 0;
68 virtual void BeforeUnloadFiredFromRenderManager(
69 bool proceed, const base::TimeTicks& proceed_time,
70 bool* proceed_to_fire_unload) = 0;
71 virtual void RenderProcessGoneFromRenderManager(
72 RenderViewHost* render_view_host) = 0;
73 virtual void UpdateRenderViewSizeForRenderManager() = 0;
74 virtual void CancelModalDialogsForRenderManager() = 0;
75 virtual void NotifySwappedFromRenderManager(
76 RenderViewHost* old_host, RenderViewHost* new_host) = 0;
77 virtual NavigationControllerImpl&
78 GetControllerForRenderManager() = 0;
80 // Create swapped out RenderViews in the given SiteInstance for each tab in
81 // the opener chain of this tab, if any. This allows the current tab to
82 // make cross-process script calls to its opener(s). Returns the route ID
83 // of the immediate opener, if one exists (otherwise MSG_ROUTING_NONE).
84 virtual int CreateOpenerRenderViewsForRenderManager(
85 SiteInstance* instance) = 0;
87 // Creates a WebUI object for the given URL if one applies. Ownership of the
88 // returned pointer will be passed to the caller. If no WebUI applies,
90 virtual WebUIImpl* CreateWebUIForRenderManager(const GURL& url) = 0;
92 // Returns the navigation entry of the current navigation, or NULL if there
94 virtual NavigationEntry*
95 GetLastCommittedNavigationEntryForRenderManager() = 0;
97 // Returns true if the location bar should be focused by default rather than
98 // the page contents. The view calls this function when the tab is focused
99 // to see what it should do.
100 virtual bool FocusLocationBarByDefault() = 0;
102 // Focuses the location bar.
103 virtual void SetFocusToLocationBar(bool select_all) = 0;
105 // Creates a view and sets the size for the specified RVH.
106 virtual void CreateViewAndSetSizeForRVH(RenderViewHost* rvh) = 0;
108 // Returns true if views created for this delegate should be created in a
110 virtual bool IsHidden() = 0;
113 virtual ~Delegate() {}
116 // Used with FrameTree::ForEach to delete RenderFrameHosts pending shutdown
117 // from a FrameTreeNode's RenderFrameHostManager. Used during destruction of
119 static bool ClearRFHsPendingShutdown(FrameTreeNode* node);
121 // All three delegate pointers must be non-NULL and are not owned by this
122 // class. They must outlive this class. The RenderViewHostDelegate and
123 // RenderWidgetHostDelegate are what will be installed into all
124 // RenderViewHosts that are created.
126 // You must call Init() before using this class.
127 RenderFrameHostManager(
128 FrameTreeNode* frame_tree_node,
129 RenderFrameHostDelegate* render_frame_delegate,
130 RenderViewHostDelegate* render_view_delegate,
131 RenderWidgetHostDelegate* render_widget_delegate,
133 virtual ~RenderFrameHostManager();
135 // For arguments, see WebContentsImpl constructor.
136 void Init(BrowserContext* browser_context,
137 SiteInstance* site_instance,
139 int frame_routing_id);
141 // Returns the currently active RenderFrameHost.
143 // This will be non-NULL between Init() and Shutdown(). You may want to NULL
144 // check it in many cases, however. Windows can send us messages during the
145 // destruction process after it has been shut down.
146 RenderFrameHostImpl* current_frame_host() const {
147 return render_frame_host_.get();
150 // TODO(creis): Remove this when we no longer use RVH for navigation.
151 RenderViewHostImpl* current_host() const;
153 // Returns the view associated with the current RenderViewHost, or NULL if
154 // there is no current one.
155 RenderWidgetHostView* GetRenderWidgetHostView() const;
157 RenderFrameProxyHost* GetProxyToParent();
159 // Returns the pending RenderFrameHost, or NULL if there is no pending one.
160 RenderFrameHostImpl* pending_frame_host() const {
161 return pending_render_frame_host_.get();
164 // TODO(creis): Remove this when we no longer use RVH for navigation.
165 RenderViewHostImpl* pending_render_view_host() const;
167 // Returns the current committed Web UI or NULL if none applies.
168 WebUIImpl* web_ui() const { return web_ui_.get(); }
170 // Returns the Web UI for the pending navigation, or NULL of none applies.
171 WebUIImpl* pending_web_ui() const {
172 return pending_web_ui_.get() ? pending_web_ui_.get() :
173 pending_and_current_web_ui_.get();
176 // Sets the pending Web UI for the pending navigation, ensuring that the
177 // bindings are appropriate for the given NavigationEntry.
178 void SetPendingWebUI(const NavigationEntryImpl& entry);
180 // Called when we want to instruct the renderer to navigate to the given
181 // navigation entry. It may create a new RenderFrameHost or re-use an existing
182 // one. The RenderFrameHost to navigate will be returned. Returns NULL if one
183 // could not be created.
184 RenderFrameHostImpl* Navigate(const NavigationEntryImpl& entry);
186 // Instructs the various live views to stop. Called when the user directed the
187 // page to stop loading.
190 // Notifies the regular and pending RenderViewHosts that a load is or is not
191 // happening. Even though the message is only for one of them, we don't know
192 // which one so we tell both.
193 void SetIsLoading(bool is_loading);
195 // Whether to close the tab or not when there is a hang during an unload
196 // handler. If we are mid-crosssite navigation, then we should proceed
197 // with the navigation instead of closing the tab.
198 bool ShouldCloseTabOnUnresponsiveRenderer();
200 // Confirms whether we should close the page or navigate away. This is called
201 // before a cross-site request or before a tab/window is closed (as indicated
202 // by the first parameter) to allow the appropriate renderer to approve or
203 // deny the request. |proceed| indicates whether the user chose to proceed.
204 // |proceed_time| is the time when the request was allowed to proceed.
205 void OnBeforeUnloadACK(bool for_cross_site_transition,
207 const base::TimeTicks& proceed_time);
209 // The |pending_render_frame_host| is ready to commit a page. We should
210 // ensure that the old RenderFrameHost runs its unload handler first and
211 // determine whether a RenderFrameHost transfer is needed.
212 // |cross_site_transferring_request| is NULL if a request is not being
213 // transferred between renderers.
214 void OnCrossSiteResponse(
215 RenderFrameHostImpl* pending_render_frame_host,
216 const GlobalRequestID& global_request_id,
217 scoped_ptr<CrossSiteTransferringRequest> cross_site_transferring_request,
218 const std::vector<GURL>& transfer_url_chain,
219 const Referrer& referrer,
220 PageTransition page_transition,
221 bool should_replace_current_entry);
223 // The RenderFrameHost has been swapped out, so we should resume the pending
224 // network response and allow the pending RenderFrameHost to commit.
225 void SwappedOut(RenderFrameHostImpl* render_frame_host);
227 // Called when a renderer's frame navigates.
228 void DidNavigateFrame(RenderFrameHostImpl* render_frame_host);
230 // Called when a renderer sets its opener to null.
231 void DidDisownOpener(RenderViewHost* render_view_host);
233 // Helper method to create and initialize a RenderFrameHost. If |swapped_out|
234 // is true, it will be initially placed on the swapped out hosts list.
235 // Otherwise, it will be used for a pending cross-site navigation.
236 // Returns the routing id of the *view* associated with the frame.
237 int CreateRenderFrame(SiteInstance* instance,
242 // Sets the passed passed interstitial as the currently showing interstitial.
243 // |interstitial_page| should be non NULL (use the remove_interstitial_page
244 // method to unset the interstitial) and no interstitial page should be set
245 // when there is already a non NULL interstitial page set.
246 void set_interstitial_page(InterstitialPageImpl* interstitial_page) {
247 DCHECK(!interstitial_page_ && interstitial_page);
248 interstitial_page_ = interstitial_page;
251 // Unsets the currently showing interstitial.
252 void remove_interstitial_page() {
253 DCHECK(interstitial_page_);
254 interstitial_page_ = NULL;
257 // Returns the currently showing interstitial, NULL if no interstitial is
259 InterstitialPageImpl* interstitial_page() const { return interstitial_page_; }
261 // NotificationObserver implementation.
262 virtual void Observe(int type,
263 const NotificationSource& source,
264 const NotificationDetails& details) OVERRIDE;
266 // Returns whether the given RenderFrameHost (or its associated
267 // RenderViewHost) is on the list of swapped out RenderFrameHosts.
268 bool IsRVHOnSwappedOutList(RenderViewHostImpl* rvh) const;
269 bool IsOnSwappedOutList(RenderFrameHostImpl* rfh) const;
271 // Returns the swapped out RenderViewHost or RenderFrameHost for the given
272 // SiteInstance, if any. This method is *deprecated* and
273 // GetRenderFrameProxyHost should be used.
274 RenderViewHostImpl* GetSwappedOutRenderViewHost(SiteInstance* instance) const;
275 RenderFrameProxyHost* GetRenderFrameProxyHost(
276 SiteInstance* instance) const;
278 // Runs the unload handler in the current page, when we know that a pending
279 // cross-process navigation is going to commit. We may initiate a transfer
280 // to a new process after this completes or times out.
281 void SwapOutOldPage();
283 // Deletes a RenderFrameHost that was pending shutdown.
284 void ClearPendingShutdownRFHForSiteInstance(int32 site_instance_id,
285 RenderFrameHostImpl* rfh);
287 // Deletes any proxy hosts associated with this node. Used during destruction
288 // of WebContentsImpl.
289 void ResetProxyHosts();
292 friend class RenderFrameHostManagerTest;
293 friend class TestWebContents;
295 // Tracks information about a navigation while a cross-process transition is
296 // in progress, in case we need to transfer it to a new RenderFrameHost.
297 // When a request is being transferred, deleting the PendingNavigationParams,
298 // and thus |cross_site_transferring_request|, will cancel the request being
299 // transferred, unless its ReleaseRequest method has been called.
300 struct PendingNavigationParams {
301 PendingNavigationParams(
302 const GlobalRequestID& global_request_id,
303 scoped_ptr<CrossSiteTransferringRequest>
304 cross_site_transferring_request,
305 const std::vector<GURL>& transfer_url,
307 PageTransition page_transition,
309 bool should_replace_current_entry);
310 ~PendingNavigationParams();
312 // The child ID and request ID for the pending navigation. Present whether
313 // |request_transfer| is NULL or not.
314 GlobalRequestID global_request_id;
316 // If a pending request needs to be transferred to another process, this
317 // owns the request until it's transferred to the new process, so it will be
318 // cleaned up if the navigation is cancelled. Otherwise, this is NULL.
319 scoped_ptr<CrossSiteTransferringRequest> cross_site_transferring_request;
321 // If |request_transfer| is non-NULL, the values below are all set.
323 // The first entry is the original request URL, and the last entry is the
324 // destination URL to request in the new process.
325 std::vector<GURL> transfer_url_chain;
327 // This is the referrer to use for the request in the new process.
330 // This is the transition type for the original navigation.
331 PageTransition page_transition;
333 // This is the frame routing ID to use in RequestTransferURL.
336 // This is whether the navigation should replace the current history entry.
337 bool should_replace_current_entry;
340 // Used with FrameTree::ForEach to erase RenderFrameProxyHosts from a
341 // FrameTreeNode's RenderFrameHostManager.
342 static bool ClearProxiesInSiteInstance(int32 site_instance_id,
343 FrameTreeNode* node);
345 // Returns whether this tab should transition to a new renderer for
346 // cross-site URLs. Enabled unless we see the --process-per-tab command line
347 // switch. Can be overridden in unit tests.
348 bool ShouldTransitionCrossSite();
350 // Returns true if for the navigation from |current_entry| to |new_entry|,
351 // a new SiteInstance and BrowsingInstance should be created (even if we are
352 // in a process model that doesn't usually swap). This forces a process swap
353 // and severs script connections with existing tabs. Cases where this can
354 // happen include transitions between WebUI and regular web pages.
355 // Either of the entries may be NULL.
356 bool ShouldSwapBrowsingInstancesForNavigation(
357 const NavigationEntry* current_entry,
358 const NavigationEntryImpl* new_entry) const;
360 // Returns true if it is safe to reuse the current WebUI when navigating from
361 // |current_entry| to |new_entry|.
362 bool ShouldReuseWebUI(
363 const NavigationEntry* current_entry,
364 const NavigationEntryImpl* new_entry) const;
366 // Returns an appropriate SiteInstance object for the given NavigationEntry,
367 // possibly reusing the current SiteInstance. If --process-per-tab is used,
368 // this is only called when ShouldSwapBrowsingInstancesForNavigation returns
370 SiteInstance* GetSiteInstanceForEntry(
371 const NavigationEntryImpl& entry,
372 SiteInstance* current_instance,
373 bool force_browsing_instance_swap);
375 // Creates a RenderFrameHost and corresponding RenderViewHost if necessary.
376 scoped_ptr<RenderFrameHostImpl> CreateRenderFrameHost(SiteInstance* instance,
378 int frame_routing_id,
382 // Sets up the necessary state for a new RenderViewHost with the given opener,
383 // if necessary. It creates a RenderFrameProxy in the target renderer process
384 // with the given |proxy_routing_id|, which is used to route IPC messages when
385 // in swapped out state. Returns early if the RenderViewHost has already been
386 // initialized for another RenderFrameHost.
387 // TODO(creis): opener_route_id is currently for the RenderViewHost but should
388 // be for the RenderFrame, since frames can have openers.
389 bool InitRenderView(RenderViewHost* render_view_host,
391 int proxy_routing_id,
392 bool for_main_frame);
394 // Sets the pending RenderFrameHost/WebUI to be the active one. Note that this
395 // doesn't require the pending render_frame_host_ pointer to be non-NULL,
396 // since there could be Web UI switching as well. Call this for every commit.
397 void CommitPending();
399 // Shutdown all RenderFrameHosts in a SiteInstance. This is called to shutdown
400 // frames when all the frames in a SiteInstance are confirmed to be swapped
402 void ShutdownRenderFrameHostsInSiteInstance(int32 site_instance_id);
404 // Helper method to terminate the pending RenderViewHost.
405 void CancelPending();
407 // Helper method to set the active RenderFrameHost. Returns the old
408 // RenderFrameHost and updates counts.
409 scoped_ptr<RenderFrameHostImpl> SetRenderFrameHost(
410 scoped_ptr<RenderFrameHostImpl> render_frame_host);
412 RenderFrameHostImpl* UpdateStateForNavigate(
413 const NavigationEntryImpl& entry);
415 // Called when a renderer process is starting to close. We should not
416 // schedule new navigations in its swapped out RenderFrameHosts after this.
417 void RendererProcessClosing(RenderProcessHost* render_process_host);
419 // For use in creating RenderFrameHosts.
420 FrameTreeNode* frame_tree_node_;
422 // Our delegate, not owned by us. Guaranteed non-NULL.
425 // Whether a navigation requiring different RenderFrameHosts is pending. This
426 // is either for cross-site requests or when required for the process type
428 bool cross_navigation_pending_;
430 // Implemented by the owner of this class. These delegates are installed into
431 // all the RenderFrameHosts that we create.
432 RenderFrameHostDelegate* render_frame_delegate_;
433 RenderViewHostDelegate* render_view_delegate_;
434 RenderWidgetHostDelegate* render_widget_delegate_;
436 // Our RenderFrameHost and its associated Web UI (if any, will be NULL for
437 // non-WebUI pages). This object is responsible for all communication with
438 // a child RenderFrame instance.
439 // For now, RenderFrameHost keeps a RenderViewHost in its SiteInstance alive.
440 // Eventually, RenderViewHost will be replaced with a page context.
441 scoped_ptr<RenderFrameHostImpl> render_frame_host_;
442 scoped_ptr<WebUIImpl> web_ui_;
444 // A RenderFrameHost used to load a cross-site page. This remains hidden
445 // while a cross-site request is pending until it calls DidNavigate. It may
446 // have an associated Web UI, in which case the Web UI pointer will be non-
449 // The |pending_web_ui_| may be non-NULL even when the
450 // |pending_render_frame_host_| is NULL. This will happen when we're
451 // transitioning between two Web UI pages: the RFH won't be swapped, so the
452 // pending pointer will be unused, but there will be a pending Web UI
453 // associated with the navigation.
454 scoped_ptr<RenderFrameHostImpl> pending_render_frame_host_;
456 // Tracks information about any current pending cross-process navigation.
457 scoped_ptr<PendingNavigationParams> pending_nav_params_;
459 // If either of these is non-NULL, the pending navigation is to a chrome:
460 // page. The scoped_ptr is used if pending_web_ui_ != web_ui_, the WeakPtr is
461 // used for when they reference the same object. If either is non-NULL, the
462 // other should be NULL.
463 scoped_ptr<WebUIImpl> pending_web_ui_;
464 base::WeakPtr<WebUIImpl> pending_and_current_web_ui_;
466 // A map of site instance ID to RenderFrameProxyHosts.
467 typedef base::hash_map<int32, RenderFrameProxyHost*> RenderFrameProxyHostMap;
468 RenderFrameProxyHostMap proxy_hosts_;
470 // A map of RenderFrameHosts pending shutdown.
471 typedef base::hash_map<int32, linked_ptr<RenderFrameHostImpl> >
473 RFHPendingDeleteMap pending_delete_hosts_;
475 // The intersitial page currently shown if any, not own by this class
476 // (the InterstitialPage is self-owned, it deletes itself when hidden).
477 InterstitialPageImpl* interstitial_page_;
479 NotificationRegistrar registrar_;
481 base::WeakPtrFactory<RenderFrameHostManager> weak_factory_;
483 DISALLOW_COPY_AND_ASSIGN(RenderFrameHostManager);
486 } // namespace content
488 #endif // CONTENT_BROWSER_FRAME_HOST_RENDER_FRAME_HOST_MANAGER_H_