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 RenderFrameHost;
35 class RenderFrameHostImpl;
36 class RenderFrameHostManagerTest;
37 class RenderFrameProxyHost;
39 class RenderViewHostImpl;
40 class RenderWidgetHostDelegate;
41 class RenderWidgetHostView;
42 class TestWebContents;
45 // Manages RenderFrameHosts for a FrameTreeNode. This class acts as a state
46 // machine to make cross-process navigations in a frame possible.
47 class CONTENT_EXPORT RenderFrameHostManager : public NotificationObserver {
49 // Functions implemented by our owner that we need.
51 // TODO(brettw) Clean this up! These are all the functions in WebContentsImpl
52 // that are required to run this class. The design should probably be better
53 // such that these are more clear.
55 // There is additional complexity that some of the functions we need in
56 // WebContentsImpl are inherited and non-virtual. These are named with
57 // "RenderManager" so that the duplicate implementation of them will be clear.
58 class CONTENT_EXPORT Delegate {
60 // Initializes the given renderer if necessary and creates the view ID
61 // corresponding to this view host. If this method is not called and the
62 // process is not shared, then the WebContentsImpl will act as though the
63 // renderer is not running (i.e., it will render "sad tab"). This method is
64 // automatically called from LoadURL. |for_main_frame_navigation| indicates
65 // whether this RenderViewHost is used to render a top-level frame, so the
66 // appropriate RenderWidgetHostView type is used.
67 virtual bool CreateRenderViewForRenderManager(
68 RenderViewHost* render_view_host,
71 bool for_main_frame_navigation) = 0;
72 virtual bool CreateRenderFrameForRenderManager(
73 RenderFrameHost* render_frame_host,
74 int parent_routing_id) = 0;
75 virtual void BeforeUnloadFiredFromRenderManager(
76 bool proceed, const base::TimeTicks& proceed_time,
77 bool* proceed_to_fire_unload) = 0;
78 virtual void RenderProcessGoneFromRenderManager(
79 RenderViewHost* render_view_host) = 0;
80 virtual void UpdateRenderViewSizeForRenderManager() = 0;
81 virtual void CancelModalDialogsForRenderManager() = 0;
82 virtual void NotifySwappedFromRenderManager(RenderFrameHost* old_host,
83 RenderFrameHost* new_host,
84 bool is_main_frame) = 0;
85 virtual NavigationControllerImpl&
86 GetControllerForRenderManager() = 0;
88 // Create swapped out RenderViews in the given SiteInstance for each tab in
89 // the opener chain of this tab, if any. This allows the current tab to
90 // make cross-process script calls to its opener(s). Returns the route ID
91 // of the immediate opener, if one exists (otherwise MSG_ROUTING_NONE).
92 virtual int CreateOpenerRenderViewsForRenderManager(
93 SiteInstance* instance) = 0;
95 // Creates a WebUI object for the given URL if one applies. Ownership of the
96 // returned pointer will be passed to the caller. If no WebUI applies,
98 virtual WebUIImpl* CreateWebUIForRenderManager(const GURL& url) = 0;
100 // Returns the navigation entry of the current navigation, or NULL if there
102 virtual NavigationEntry*
103 GetLastCommittedNavigationEntryForRenderManager() = 0;
105 // Returns true if the location bar should be focused by default rather than
106 // the page contents. The view calls this function when the tab is focused
107 // to see what it should do.
108 virtual bool FocusLocationBarByDefault() = 0;
110 // Focuses the location bar.
111 virtual void SetFocusToLocationBar(bool select_all) = 0;
113 // Creates a view and sets the size for the specified RVH.
114 virtual void CreateViewAndSetSizeForRVH(RenderViewHost* rvh) = 0;
116 // Returns true if views created for this delegate should be created in a
118 virtual bool IsHidden() = 0;
121 virtual ~Delegate() {}
124 // Used with FrameTree::ForEach to delete RenderFrameHosts pending shutdown
125 // from a FrameTreeNode's RenderFrameHostManager. Used during destruction of
127 static bool ClearRFHsPendingShutdown(FrameTreeNode* node);
129 // All three delegate pointers must be non-NULL and are not owned by this
130 // class. They must outlive this class. The RenderViewHostDelegate and
131 // RenderWidgetHostDelegate are what will be installed into all
132 // RenderViewHosts that are created.
134 // You must call Init() before using this class.
135 RenderFrameHostManager(
136 FrameTreeNode* frame_tree_node,
137 RenderFrameHostDelegate* render_frame_delegate,
138 RenderViewHostDelegate* render_view_delegate,
139 RenderWidgetHostDelegate* render_widget_delegate,
141 virtual ~RenderFrameHostManager();
143 // For arguments, see WebContentsImpl constructor.
144 void Init(BrowserContext* browser_context,
145 SiteInstance* site_instance,
147 int frame_routing_id);
149 // Returns the currently active RenderFrameHost.
151 // This will be non-NULL between Init() and Shutdown(). You may want to NULL
152 // check it in many cases, however. Windows can send us messages during the
153 // destruction process after it has been shut down.
154 RenderFrameHostImpl* current_frame_host() const {
155 return render_frame_host_.get();
158 // TODO(creis): Remove this when we no longer use RVH for navigation.
159 RenderViewHostImpl* current_host() const;
161 // Returns the view associated with the current RenderViewHost, or NULL if
162 // there is no current one.
163 RenderWidgetHostView* GetRenderWidgetHostView() const;
165 RenderFrameProxyHost* GetProxyToParent();
167 // Returns the pending RenderFrameHost, or NULL if there is no pending one.
168 RenderFrameHostImpl* pending_frame_host() const {
169 return pending_render_frame_host_.get();
172 // TODO(creis): Remove this when we no longer use RVH for navigation.
173 RenderViewHostImpl* pending_render_view_host() const;
175 // Returns the current committed Web UI or NULL if none applies.
176 WebUIImpl* web_ui() const { return web_ui_.get(); }
178 // Returns the Web UI for the pending navigation, or NULL of none applies.
179 WebUIImpl* pending_web_ui() const {
180 return pending_web_ui_.get() ? pending_web_ui_.get() :
181 pending_and_current_web_ui_.get();
184 // Sets the pending Web UI for the pending navigation, ensuring that the
185 // bindings are appropriate for the given NavigationEntry.
186 void SetPendingWebUI(const NavigationEntryImpl& entry);
188 // Called when we want to instruct the renderer to navigate to the given
189 // navigation entry. It may create a new RenderFrameHost or re-use an existing
190 // one. The RenderFrameHost to navigate will be returned. Returns NULL if one
191 // could not be created.
192 RenderFrameHostImpl* Navigate(const NavigationEntryImpl& entry);
194 // Instructs the various live views to stop. Called when the user directed the
195 // page to stop loading.
198 // Notifies the regular and pending RenderViewHosts that a load is or is not
199 // happening. Even though the message is only for one of them, we don't know
200 // which one so we tell both.
201 void SetIsLoading(bool is_loading);
203 // Whether to close the tab or not when there is a hang during an unload
204 // handler. If we are mid-crosssite navigation, then we should proceed
205 // with the navigation instead of closing the tab.
206 bool ShouldCloseTabOnUnresponsiveRenderer();
208 // Confirms whether we should close the page or navigate away. This is called
209 // before a cross-site request or before a tab/window is closed (as indicated
210 // by the first parameter) to allow the appropriate renderer to approve or
211 // deny the request. |proceed| indicates whether the user chose to proceed.
212 // |proceed_time| is the time when the request was allowed to proceed.
213 void OnBeforeUnloadACK(bool for_cross_site_transition,
215 const base::TimeTicks& proceed_time);
217 // The |pending_render_frame_host| is ready to commit a page. We should
218 // ensure that the old RenderFrameHost runs its unload handler first and
219 // determine whether a RenderFrameHost transfer is needed.
220 // |cross_site_transferring_request| is NULL if a request is not being
221 // transferred between renderers.
222 void OnCrossSiteResponse(
223 RenderFrameHostImpl* pending_render_frame_host,
224 const GlobalRequestID& global_request_id,
225 scoped_ptr<CrossSiteTransferringRequest> cross_site_transferring_request,
226 const std::vector<GURL>& transfer_url_chain,
227 const Referrer& referrer,
228 PageTransition page_transition,
229 bool should_replace_current_entry);
231 // Received a response from CrossSiteResourceHandler. If the navigation
232 // specifies a transition, this is called and the navigation will not resume
233 // until ResumeResponseDeferredAtStart.
234 void OnDeferredAfterResponseStarted(
235 const GlobalRequestID& global_request_id,
236 RenderFrameHostImpl* pending_render_frame_host);
238 // Resume navigation paused after receiving response headers.
239 void ResumeResponseDeferredAtStart();
241 // The RenderFrameHost has been swapped out, so we should resume the pending
242 // network response and allow the pending RenderFrameHost to commit.
243 void SwappedOut(RenderFrameHostImpl* render_frame_host);
245 // Called when a renderer's frame navigates.
246 void DidNavigateFrame(RenderFrameHostImpl* render_frame_host);
248 // Called when a renderer sets its opener to null.
249 void DidDisownOpener(RenderViewHost* render_view_host);
251 // Helper method to create and initialize a RenderFrameHost. If |swapped_out|
252 // is true, it will be initially placed on the swapped out hosts list.
253 // Otherwise, it will be used for a pending cross-site navigation.
254 // Returns the routing id of the *view* associated with the frame.
255 int CreateRenderFrame(SiteInstance* instance,
258 bool for_main_frame_navigation,
261 // Helper method to create and initialize a RenderFrameProxyHost and return
263 int CreateRenderFrameProxy(SiteInstance* instance);
265 // Sets the passed passed interstitial as the currently showing interstitial.
266 // |interstitial_page| should be non NULL (use the remove_interstitial_page
267 // method to unset the interstitial) and no interstitial page should be set
268 // when there is already a non NULL interstitial page set.
269 void set_interstitial_page(InterstitialPageImpl* interstitial_page) {
270 DCHECK(!interstitial_page_ && interstitial_page);
271 interstitial_page_ = interstitial_page;
274 // Unsets the currently showing interstitial.
275 void remove_interstitial_page() {
276 DCHECK(interstitial_page_);
277 interstitial_page_ = NULL;
280 // Returns the currently showing interstitial, NULL if no interstitial is
282 InterstitialPageImpl* interstitial_page() const { return interstitial_page_; }
284 // NotificationObserver implementation.
285 virtual void Observe(int type,
286 const NotificationSource& source,
287 const NotificationDetails& details) OVERRIDE;
289 // Returns whether the given RenderFrameHost (or its associated
290 // RenderViewHost) is on the list of swapped out RenderFrameHosts.
291 bool IsRVHOnSwappedOutList(RenderViewHostImpl* rvh) const;
292 bool IsOnSwappedOutList(RenderFrameHostImpl* rfh) const;
294 // Returns the swapped out RenderViewHost or RenderFrameHost for the given
295 // SiteInstance, if any. This method is *deprecated* and
296 // GetRenderFrameProxyHost should be used.
297 RenderViewHostImpl* GetSwappedOutRenderViewHost(SiteInstance* instance) const;
298 RenderFrameProxyHost* GetRenderFrameProxyHost(
299 SiteInstance* instance) const;
301 // Runs the unload handler in the current page, when we know that a pending
302 // cross-process navigation is going to commit. We may initiate a transfer
303 // to a new process after this completes or times out.
304 void SwapOutOldPage();
306 // Deletes a RenderFrameHost that was pending shutdown.
307 void ClearPendingShutdownRFHForSiteInstance(int32 site_instance_id,
308 RenderFrameHostImpl* rfh);
310 // Deletes any proxy hosts associated with this node. Used during destruction
311 // of WebContentsImpl.
312 void ResetProxyHosts();
314 // Used to start a navigation, part of browser-side navigation project.
315 void OnBeginNavigation(const FrameHostMsg_BeginNavigation_Params& params);
317 // Returns the routing id for a RenderFrameHost or RenderFrameHostProxy
318 // that has the given SiteInstance and is associated with this
319 // RenderFrameHostManager. Returns MSG_ROUTING_NONE if none is found.
320 int GetRoutingIdForSiteInstance(SiteInstance* site_instance);
323 friend class RenderFrameHostManagerTest;
324 friend class TestWebContents;
326 FRIEND_TEST_ALL_PREFIXES(CrossProcessFrameTreeBrowserTest,
327 CreateCrossProcessSubframeProxies);
329 // Tracks information about a navigation while a cross-process transition is
330 // in progress, in case we need to transfer it to a new RenderFrameHost.
331 // When a request is being transferred, deleting the PendingNavigationParams,
332 // and thus |cross_site_transferring_request|, will cancel the request being
333 // transferred, unless its ReleaseRequest method has been called.
334 struct PendingNavigationParams {
335 PendingNavigationParams(
336 const GlobalRequestID& global_request_id,
337 scoped_ptr<CrossSiteTransferringRequest>
338 cross_site_transferring_request,
339 const std::vector<GURL>& transfer_url,
341 PageTransition page_transition,
343 bool should_replace_current_entry);
344 ~PendingNavigationParams();
346 // The child ID and request ID for the pending navigation. Present whether
347 // |request_transfer| is NULL or not.
348 GlobalRequestID global_request_id;
350 // If a pending request needs to be transferred to another process, this
351 // owns the request until it's transferred to the new process, so it will be
352 // cleaned up if the navigation is cancelled. Otherwise, this is NULL.
353 scoped_ptr<CrossSiteTransferringRequest> cross_site_transferring_request;
355 // If |request_transfer| is non-NULL, the values below are all set.
357 // The first entry is the original request URL, and the last entry is the
358 // destination URL to request in the new process.
359 std::vector<GURL> transfer_url_chain;
361 // This is the referrer to use for the request in the new process.
364 // This is the transition type for the original navigation.
365 PageTransition page_transition;
367 // This is the frame routing ID to use in RequestTransferURL.
370 // This is whether the navigation should replace the current history entry.
371 bool should_replace_current_entry;
374 // Returns the current navigation request (used in the PlzNavigate navigation
375 // logic refactoring project).
376 NavigationRequest* navigation_request_for_testing() const {
377 return navigation_request_.get(); }
379 // Used with FrameTree::ForEach to erase RenderFrameProxyHosts from a
380 // FrameTreeNode's RenderFrameHostManager.
381 static bool ClearProxiesInSiteInstance(int32 site_instance_id,
382 FrameTreeNode* node);
384 // Returns whether this tab should transition to a new renderer for
385 // cross-site URLs. Enabled unless we see the --process-per-tab command line
386 // switch. Can be overridden in unit tests.
387 bool ShouldTransitionCrossSite();
389 // Returns true if for the navigation from |current_effective_url| to
390 // |new_effective_url|, a new SiteInstance and BrowsingInstance should be
391 // created (even if we are in a process model that doesn't usually swap).
392 // This forces a process swap and severs script connections with existing
393 // tabs. Cases where this can happen include transitions between WebUI and
394 // regular web pages. |new_site_instance| may be null.
395 // If there is no current NavigationEntry, then |current_is_view_source_mode|
396 // should be the same as |new_is_view_source_mode|.
398 // We use the effective URL here, since that's what is used in the
399 // SiteInstance's site and when we later call IsSameWebSite. If there is no
400 // current NavigationEntry, check the current SiteInstance's site, which might
401 // already be committed to a Web UI URL (such as the NTP).
402 bool ShouldSwapBrowsingInstancesForNavigation(
403 const GURL& current_effective_url,
404 bool current_is_view_source_mode,
405 SiteInstance* new_site_instance,
406 const GURL& new_effective_url,
407 bool new_is_view_source_mode) const;
409 // Returns true if it is safe to reuse the current WebUI when navigating from
410 // |current_entry| to |new_entry|.
411 bool ShouldReuseWebUI(
412 const NavigationEntry* current_entry,
413 const NavigationEntryImpl* new_entry) const;
415 // Returns an appropriate SiteInstance object for the given |dest_url|,
416 // possibly reusing the current SiteInstance. If --process-per-tab is used,
417 // this is only called when ShouldSwapBrowsingInstancesForNavigation returns
418 // true. |dest_instance| will be used if it is not null.
419 SiteInstance* GetSiteInstanceForURL(
420 const GURL& dest_url,
421 SiteInstance* dest_instance,
422 PageTransition dest_transition,
423 bool dest_is_restore,
424 bool dest_is_view_source_mode,
425 SiteInstance* current_instance,
426 bool force_browsing_instance_swap);
428 // Creates a RenderFrameHost and corresponding RenderViewHost if necessary.
429 scoped_ptr<RenderFrameHostImpl> CreateRenderFrameHost(SiteInstance* instance,
431 int frame_routing_id,
435 // Sets up the necessary state for a new RenderViewHost with the given opener,
436 // if necessary. It creates a RenderFrameProxy in the target renderer process
437 // with the given |proxy_routing_id|, which is used to route IPC messages when
438 // in swapped out state. Returns early if the RenderViewHost has already been
439 // initialized for another RenderFrameHost.
440 // TODO(creis): opener_route_id is currently for the RenderViewHost but should
441 // be for the RenderFrame, since frames can have openers.
442 bool InitRenderView(RenderViewHost* render_view_host,
444 int proxy_routing_id,
445 bool for_main_frame_navigation);
447 // Initialization for RenderFrameHost uses the same sequence as InitRenderView
449 bool InitRenderFrame(RenderFrameHost* render_frame_host);
451 // Sets the pending RenderFrameHost/WebUI to be the active one. Note that this
452 // doesn't require the pending render_frame_host_ pointer to be non-NULL,
453 // since there could be Web UI switching as well. Call this for every commit.
454 void CommitPending();
456 // Shutdown all RenderFrameProxyHosts in a SiteInstance. This is called to
457 // shutdown frames when all the frames in a SiteInstance are confirmed to be
459 void ShutdownRenderFrameProxyHostsInSiteInstance(int32 site_instance_id);
461 // Helper method to terminate the pending RenderViewHost.
462 void CancelPending();
464 // Helper method to set the active RenderFrameHost. Returns the old
465 // RenderFrameHost and updates counts.
466 scoped_ptr<RenderFrameHostImpl> SetRenderFrameHost(
467 scoped_ptr<RenderFrameHostImpl> render_frame_host);
469 RenderFrameHostImpl* UpdateStateForNavigate(
470 const NavigationEntryImpl& entry);
472 // Called when a renderer process is starting to close. We should not
473 // schedule new navigations in its swapped out RenderFrameHosts after this.
474 void RendererProcessClosing(RenderProcessHost* render_process_host);
476 // Helper method to delete a RenderFrameProxyHost from the list, if one exists
477 // for the given |instance|.
478 void DeleteRenderFrameProxyHost(SiteInstance* instance);
480 // For use in creating RenderFrameHosts.
481 FrameTreeNode* frame_tree_node_;
483 // Our delegate, not owned by us. Guaranteed non-NULL.
486 // Whether a navigation requiring different RenderFrameHosts is pending. This
487 // is either for cross-site requests or when required for the process type
489 bool cross_navigation_pending_;
491 // Implemented by the owner of this class. These delegates are installed into
492 // all the RenderFrameHosts that we create.
493 RenderFrameHostDelegate* render_frame_delegate_;
494 RenderViewHostDelegate* render_view_delegate_;
495 RenderWidgetHostDelegate* render_widget_delegate_;
497 // Our RenderFrameHost and its associated Web UI (if any, will be NULL for
498 // non-WebUI pages). This object is responsible for all communication with
499 // a child RenderFrame instance.
500 // For now, RenderFrameHost keeps a RenderViewHost in its SiteInstance alive.
501 // Eventually, RenderViewHost will be replaced with a page context.
502 scoped_ptr<RenderFrameHostImpl> render_frame_host_;
503 scoped_ptr<WebUIImpl> web_ui_;
505 // A RenderFrameHost used to load a cross-site page. This remains hidden
506 // while a cross-site request is pending until it calls DidNavigate. It may
507 // have an associated Web UI, in which case the Web UI pointer will be non-
510 // The |pending_web_ui_| may be non-NULL even when the
511 // |pending_render_frame_host_| is NULL. This will happen when we're
512 // transitioning between two Web UI pages: the RFH won't be swapped, so the
513 // pending pointer will be unused, but there will be a pending Web UI
514 // associated with the navigation.
515 scoped_ptr<RenderFrameHostImpl> pending_render_frame_host_;
517 // Tracks information about any current pending cross-process navigation.
518 scoped_ptr<PendingNavigationParams> pending_nav_params_;
520 // Tracks information about any navigation paused after receiving response
522 scoped_ptr<GlobalRequestID> response_started_id_;
524 // If either of these is non-NULL, the pending navigation is to a chrome:
525 // page. The scoped_ptr is used if pending_web_ui_ != web_ui_, the WeakPtr is
526 // used for when they reference the same object. If either is non-NULL, the
527 // other should be NULL.
528 scoped_ptr<WebUIImpl> pending_web_ui_;
529 base::WeakPtr<WebUIImpl> pending_and_current_web_ui_;
531 // A map of site instance ID to RenderFrameProxyHosts.
532 typedef base::hash_map<int32, RenderFrameProxyHost*> RenderFrameProxyHostMap;
533 RenderFrameProxyHostMap proxy_hosts_;
535 // A map of RenderFrameHosts pending shutdown.
536 typedef base::hash_map<int32, linked_ptr<RenderFrameHostImpl> >
538 RFHPendingDeleteMap pending_delete_hosts_;
540 // The intersitial page currently shown if any, not own by this class
541 // (the InterstitialPage is self-owned, it deletes itself when hidden).
542 InterstitialPageImpl* interstitial_page_;
544 NotificationRegistrar registrar_;
546 // Owns a navigation request that originated in that frame until it commits.
547 scoped_ptr<NavigationRequest> navigation_request_;
549 base::WeakPtrFactory<RenderFrameHostManager> weak_factory_;
551 DISALLOW_COPY_AND_ASSIGN(RenderFrameHostManager);
554 } // namespace content
556 #endif // CONTENT_BROWSER_FRAME_HOST_RENDER_FRAME_HOST_MANAGER_H_