base/threading: remove ScopedTracker placed for experiments
[chromium-blink-merge.git] / content / browser / frame_host / render_frame_host_manager.h
blobb58673e8aea3e94c123f585a6759d070878e8c75
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 <list>
9 #include <map>
11 #include "base/basictypes.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "content/browser/renderer_host/render_view_host_delegate.h"
16 #include "content/browser/site_instance_impl.h"
17 #include "content/common/content_export.h"
18 #include "content/public/browser/global_request_id.h"
19 #include "content/public/common/referrer.h"
20 #include "ui/base/page_transition_types.h"
21 #include "url/origin.h"
23 namespace content {
24 class BrowserContext;
25 class CrossProcessFrameConnector;
26 class CrossSiteTransferringRequest;
27 class FrameNavigationEntry;
28 class FrameTreeNode;
29 class InterstitialPageImpl;
30 class NavigationControllerImpl;
31 class NavigationEntry;
32 class NavigationEntryImpl;
33 class NavigationHandleImpl;
34 class NavigationRequest;
35 class NavigatorTestWithBrowserSideNavigation;
36 class RenderFrameHost;
37 class RenderFrameHostDelegate;
38 class RenderFrameHostImpl;
39 class RenderFrameHostManagerTest;
40 class RenderFrameProxyHost;
41 class RenderViewHost;
42 class RenderViewHostImpl;
43 class RenderWidgetHostDelegate;
44 class RenderWidgetHostView;
45 class TestWebContents;
46 class WebUIImpl;
47 struct CommonNavigationParams;
48 struct FrameReplicationState;
50 // Manages RenderFrameHosts for a FrameTreeNode. It maintains a
51 // current_frame_host() which is the content currently visible to the user. When
52 // a frame is told to navigate to a different web site (as determined by
53 // SiteInstance), it will replace its current RenderFrameHost with a new
54 // RenderFrameHost dedicated to the new SiteInstance, possibly in a new process.
56 // Cross-process navigation works like this:
58 // - RFHM::Navigate determines whether the destination is cross-site, and if so,
59 // it creates a pending_render_frame_host_.
61 // - The pending RFH is created in the "navigations suspended" state, meaning no
62 // navigation messages are sent to its renderer until the beforeunload handler
63 // has a chance to run in the current RFH.
65 // - The current RFH runs its beforeunload handler. If it returns false, we
66 // cancel all the pending logic. Otherwise we allow the pending RFH to send
67 // the navigation request to its renderer.
69 // - ResourceDispatcherHost receives a ResourceRequest on the IO thread for the
70 // main resource load from the pending RFH. It creates a
71 // CrossSiteResourceHandler to check whether a process transfer is needed when
72 // the request is ready to commit.
74 // - When RDH receives a response, the MimeTypeResourceHandler determines
75 // whether it is a navigation type that doesn't commit (e.g. download, 204 or
76 // error page). If so, it sends a message to the new renderer causing it to
77 // cancel the request, and the request (e.g. the download) proceeds. In this
78 // case, the pending RFH will never become the current RFH, but it remains
79 // until the next DidNavigate event for this WebContentsImpl.
81 // - After RDH receives a response and determines that it is safe and not a
82 // download, the CrossSiteResourceHandler checks whether a transfer for a
83 // redirect is needed. If so, it pauses the network response and starts an
84 // identical navigation in a new pending RFH. When the identical request is
85 // later received by RDH, the response is transferred and unpaused.
87 // - Otherwise, the network response commits in the pending RFH's renderer,
88 // which sends a DidCommitProvisionalLoad message back to the browser process.
90 // - RFHM::CommitPending makes visible the new RFH, and initiates the unload
91 // handler in the old RFH. The unload handler will complete in the background.
93 // - RenderFrameHostManager may keep the previous RFH alive as a
94 // RenderFrameProxyHost, to be used (for example) if the user goes back. The
95 // process only stays live if another tab is using it, but if so, the existing
96 // frame relationships will be maintained.
97 class CONTENT_EXPORT RenderFrameHostManager {
98 public:
99 // Functions implemented by our owner that we need.
101 // TODO(brettw) Clean this up! These are all the functions in WebContentsImpl
102 // that are required to run this class. The design should probably be better
103 // such that these are more clear.
105 // There is additional complexity that some of the functions we need in
106 // WebContentsImpl are inherited and non-virtual. These are named with
107 // "RenderManager" so that the duplicate implementation of them will be clear.
109 // Functions and parameters whose description are prefixed by PlzNavigate are
110 // part of a navigation refactoring project, currently behind the
111 // enable-browser-side-navigation flag. The idea is to move the logic behind
112 // driving navigations from the renderer to the browser.
113 class CONTENT_EXPORT Delegate {
114 public:
115 // Initializes the given renderer if necessary and creates the view ID
116 // corresponding to this view host. If this method is not called and the
117 // process is not shared, then the WebContentsImpl will act as though the
118 // renderer is not running (i.e., it will render "sad tab"). This method is
119 // automatically called from LoadURL. |for_main_frame_navigation| indicates
120 // whether this RenderViewHost is used to render a top-level frame, so the
121 // appropriate RenderWidgetHostView type is used.
122 virtual bool CreateRenderViewForRenderManager(
123 RenderViewHost* render_view_host,
124 int opener_frame_routing_id,
125 int proxy_routing_id,
126 const FrameReplicationState& replicated_frame_state,
127 bool for_main_frame_navigation) = 0;
128 virtual bool CreateRenderFrameForRenderManager(
129 RenderFrameHost* render_frame_host,
130 int parent_routing_id,
131 int previous_sibling_routing_id,
132 int proxy_routing_id) = 0;
133 virtual void BeforeUnloadFiredFromRenderManager(
134 bool proceed, const base::TimeTicks& proceed_time,
135 bool* proceed_to_fire_unload) = 0;
136 virtual void RenderProcessGoneFromRenderManager(
137 RenderViewHost* render_view_host) = 0;
138 virtual void UpdateRenderViewSizeForRenderManager() = 0;
139 virtual void CancelModalDialogsForRenderManager() = 0;
140 virtual void NotifySwappedFromRenderManager(RenderFrameHost* old_host,
141 RenderFrameHost* new_host,
142 bool is_main_frame) = 0;
143 // TODO(nasko): This should be removed once extensions no longer use
144 // NotificationService. See https://crbug.com/462682.
145 virtual void NotifyMainFrameSwappedFromRenderManager(
146 RenderViewHost* old_host,
147 RenderViewHost* new_host) = 0;
148 virtual NavigationControllerImpl&
149 GetControllerForRenderManager() = 0;
151 // Creates a WebUI object for the given URL if one applies. Ownership of the
152 // returned pointer will be passed to the caller. If no WebUI applies,
153 // returns NULL.
154 virtual scoped_ptr<WebUIImpl> CreateWebUIForRenderManager(
155 const GURL& url) = 0;
157 // Returns the navigation entry of the current navigation, or NULL if there
158 // is none.
159 virtual NavigationEntry*
160 GetLastCommittedNavigationEntryForRenderManager() = 0;
162 // Returns true if the location bar should be focused by default rather than
163 // the page contents. The view calls this function when the tab is focused
164 // to see what it should do.
165 virtual bool FocusLocationBarByDefault() = 0;
167 // Focuses the location bar.
168 virtual void SetFocusToLocationBar(bool select_all) = 0;
170 // Returns true if views created for this delegate should be created in a
171 // hidden state.
172 virtual bool IsHidden() = 0;
174 // If the delegate is an inner WebContents, this method returns the
175 // FrameTreeNode ID of the frame in the outer WebContents which hosts
176 // the inner WebContents. Returns FrameTreeNode::kFrameTreeNodeInvalidID
177 // if the delegate does not have an outer WebContents.
178 virtual int GetOuterDelegateFrameTreeNodeID() = 0;
180 protected:
181 virtual ~Delegate() {}
184 // Used with FrameTree::ForEach to delete RenderFrameHosts pending shutdown
185 // from a FrameTreeNode's RenderFrameHostManager. Used during destruction of
186 // WebContentsImpl.
187 static bool ClearRFHsPendingShutdown(FrameTreeNode* node);
189 // All three delegate pointers must be non-NULL and are not owned by this
190 // class. They must outlive this class. The RenderViewHostDelegate and
191 // RenderWidgetHostDelegate are what will be installed into all
192 // RenderViewHosts that are created.
194 // You must call Init() before using this class.
195 RenderFrameHostManager(
196 FrameTreeNode* frame_tree_node,
197 RenderFrameHostDelegate* render_frame_delegate,
198 RenderViewHostDelegate* render_view_delegate,
199 RenderWidgetHostDelegate* render_widget_delegate,
200 Delegate* delegate);
201 ~RenderFrameHostManager();
203 // For arguments, see WebContentsImpl constructor.
204 void Init(BrowserContext* browser_context,
205 SiteInstance* site_instance,
206 int view_routing_id,
207 int frame_routing_id);
209 // Returns the currently active RenderFrameHost.
211 // This will be non-NULL between Init() and Shutdown(). You may want to NULL
212 // check it in many cases, however. Windows can send us messages during the
213 // destruction process after it has been shut down.
214 RenderFrameHostImpl* current_frame_host() const {
215 return render_frame_host_.get();
218 // TODO(creis): Remove this when we no longer use RVH for navigation.
219 RenderViewHostImpl* current_host() const;
221 // Returns the view associated with the current RenderViewHost, or NULL if
222 // there is no current one.
223 RenderWidgetHostView* GetRenderWidgetHostView() const;
225 // Returns whether this manager belongs to a FrameTreeNode that belongs to an
226 // inner WebContents.
227 bool ForInnerDelegate();
229 // Returns the RenderWidgetHost of the outer WebContents (if any) that can be
230 // used to fetch the last keyboard event.
231 // TODO(lazyboy): This can be removed once input events are sent directly to
232 // remote frames.
233 RenderWidgetHostImpl* GetOuterRenderWidgetHostForKeyboardInput();
235 RenderFrameProxyHost* GetProxyToParent();
237 // Returns the proxy to inner WebContents in the outer WebContents's
238 // SiteInstance. Returns nullptr if this WebContents isn't part of inner/outer
239 // relationship.
240 RenderFrameProxyHost* GetProxyToOuterDelegate();
242 // Removes the FrameTreeNode in the outer WebContents that represents this
243 // FrameTreeNode.
244 // TODO(lazyboy): This does not belong to RenderFrameHostManager, move it to
245 // somehwere else.
246 void RemoveOuterDelegateFrame();
248 // Returns the pending RenderFrameHost, or NULL if there is no pending one.
249 RenderFrameHostImpl* pending_frame_host() const {
250 return pending_render_frame_host_.get();
253 // Returns the speculative RenderFrameHost, or null if there is no speculative
254 // one.
255 RenderFrameHostImpl* speculative_frame_host() const {
256 return speculative_render_frame_host_.get();
259 // TODO(creis): Remove this when we no longer use RVH for navigation.
260 RenderViewHostImpl* pending_render_view_host() const;
262 // Returns the current committed Web UI or NULL if none applies.
263 WebUIImpl* web_ui() const { return web_ui_.get(); }
265 // Returns the Web UI for the pending navigation, or NULL of none applies.
266 WebUIImpl* pending_web_ui() const {
267 return pending_web_ui_.get() ? pending_web_ui_.get() :
268 pending_and_current_web_ui_.get();
271 // PlzNavigate
272 // Returns the speculative WebUI for the navigation (a newly created one or
273 // the current one if it should be reused). If none is set returns nullptr.
274 WebUIImpl* speculative_web_ui() const {
275 return should_reuse_web_ui_ ? web_ui_.get() : speculative_web_ui_.get();
278 // Called when we want to instruct the renderer to navigate to the given
279 // navigation entry. It may create a new RenderFrameHost or re-use an existing
280 // one. The RenderFrameHost to navigate will be returned. Returns NULL if one
281 // could not be created. |dest_url| takes precedence over the |frame_entry|'s
282 // url (this is necessary because ReloadOriginalRequest navigates to a
283 // different URL than the last committed entry, without modifying it).
284 RenderFrameHostImpl* Navigate(const GURL& dest_url,
285 const FrameNavigationEntry& frame_entry,
286 const NavigationEntryImpl& entry);
288 // Instructs the various live views to stop. Called when the user directed the
289 // page to stop loading.
290 void Stop();
292 // Notifies the regular and pending RenderViewHosts that a load is or is not
293 // happening. Even though the message is only for one of them, we don't know
294 // which one so we tell both.
295 void SetIsLoading(bool is_loading);
297 // Whether to close the tab or not when there is a hang during an unload
298 // handler. If we are mid-crosssite navigation, then we should proceed
299 // with the navigation instead of closing the tab.
300 bool ShouldCloseTabOnUnresponsiveRenderer();
302 // Confirms whether we should close the page or navigate away. This is called
303 // before a cross-site request or before a tab/window is closed (as indicated
304 // by the first parameter) to allow the appropriate renderer to approve or
305 // deny the request. |proceed| indicates whether the user chose to proceed.
306 // |proceed_time| is the time when the request was allowed to proceed.
307 void OnBeforeUnloadACK(bool for_cross_site_transition,
308 bool proceed,
309 const base::TimeTicks& proceed_time);
311 // The |pending_render_frame_host| is ready to commit a page. We should
312 // ensure that the old RenderFrameHost runs its unload handler first and
313 // determine whether a RenderFrameHost transfer is needed.
314 // |cross_site_transferring_request| is NULL if a request is not being
315 // transferred between renderers.
316 void OnCrossSiteResponse(
317 RenderFrameHostImpl* pending_render_frame_host,
318 const GlobalRequestID& global_request_id,
319 scoped_ptr<CrossSiteTransferringRequest> cross_site_transferring_request,
320 const std::vector<GURL>& transfer_url_chain,
321 const Referrer& referrer,
322 ui::PageTransition page_transition,
323 bool should_replace_current_entry);
325 // Called when a renderer's frame navigates.
326 void DidNavigateFrame(RenderFrameHostImpl* render_frame_host,
327 bool was_caused_by_user_gesture);
329 // Called when a renderer sets its opener to null.
330 void DidDisownOpener(RenderFrameHost* render_frame_host);
332 // Sets the pending Web UI for the pending navigation, ensuring that the
333 // bindings are appropriate compared to |bindings|.
334 void SetPendingWebUI(const GURL& url, int bindings);
336 // Creates and initializes a RenderFrameHost. The |web_ui| is an optional
337 // input parameter used to double check bindings when swapping back in a
338 // previously existing RenderFrameHost. If |flags| has the
339 // CREATE_RF_SWAPPED_OUT bit set from the CreateRenderFrameFlags enum, it will
340 // initially be placed on the swapped out hosts list. If |view_routing_id_ptr|
341 // is not nullptr it will be set to the routing id of the view associated with
342 // the frame.
343 scoped_ptr<RenderFrameHostImpl> CreateRenderFrame(SiteInstance* instance,
344 WebUIImpl* web_ui,
345 int flags,
346 int* view_routing_id_ptr);
348 // Helper method to create and initialize a RenderFrameProxyHost and return
349 // its routing id.
350 int CreateRenderFrameProxy(SiteInstance* instance);
352 // Creates proxies for a new child frame at FrameTreeNode |child| in all
353 // SiteInstances for which the current frame has proxies. This method is
354 // called on the parent of a new child frame before the child leaves the
355 // SiteInstance.
356 void CreateProxiesForChildFrame(FrameTreeNode* child);
358 // Sets the passed passed interstitial as the currently showing interstitial.
359 // |interstitial_page| should be non NULL (use the remove_interstitial_page
360 // method to unset the interstitial) and no interstitial page should be set
361 // when there is already a non NULL interstitial page set.
362 void set_interstitial_page(InterstitialPageImpl* interstitial_page) {
363 DCHECK(!interstitial_page_ && interstitial_page);
364 interstitial_page_ = interstitial_page;
367 // Unsets the currently showing interstitial.
368 void remove_interstitial_page() {
369 DCHECK(interstitial_page_);
370 interstitial_page_ = NULL;
373 // Returns the currently showing interstitial, NULL if no interstitial is
374 // showing.
375 InterstitialPageImpl* interstitial_page() const { return interstitial_page_; }
377 // Returns whether the given RenderFrameHost (or its associated
378 // RenderViewHost) is on the list of swapped out RenderFrameHosts.
379 bool IsRVHOnSwappedOutList(RenderViewHostImpl* rvh) const;
380 bool IsOnSwappedOutList(RenderFrameHostImpl* rfh) const;
382 // Returns the swapped out RenderViewHost for the given SiteInstance, if any.
383 // This method is *deprecated* and GetRenderFrameProxyHost should be used.
384 RenderViewHostImpl* GetSwappedOutRenderViewHost(SiteInstance* instance) const;
386 // Returns the RenderFrameProxyHost for the given SiteInstance, if any.
387 RenderFrameProxyHost* GetRenderFrameProxyHost(
388 SiteInstance* instance) const;
390 // Returns whether |render_frame_host| is on the pending deletion list.
391 bool IsPendingDeletion(RenderFrameHostImpl* render_frame_host);
393 // If |render_frame_host| is on the pending deletion list, this deletes it.
394 // Returns whether it was deleted.
395 bool DeleteFromPendingList(RenderFrameHostImpl* render_frame_host);
397 // Deletes any proxy hosts associated with this node. Used during destruction
398 // of WebContentsImpl.
399 void ResetProxyHosts();
401 // Returns the routing id for a RenderFrameHost or RenderFrameProxyHost
402 // that has the given SiteInstance and is associated with this
403 // RenderFrameHostManager. Returns MSG_ROUTING_NONE if none is found.
404 int GetRoutingIdForSiteInstance(SiteInstance* site_instance);
406 // PlzNavigate
407 // Notifies the RenderFrameHostManager that a new NavigationRequest has been
408 // created and set in the FrameTreeNode so that it can speculatively create a
409 // new RenderFrameHost (and potentially a new process) if needed.
410 void DidCreateNavigationRequest(const NavigationRequest& request);
412 // PlzNavigate
413 // Called (possibly several times) during a navigation to select or create an
414 // appropriate RenderFrameHost for the provided URL. The returned pointer will
415 // be for the current or the speculative RenderFrameHost and the instance is
416 // owned by this manager.
417 RenderFrameHostImpl* GetFrameHostForNavigation(
418 const NavigationRequest& request);
420 // PlzNavigate
421 // Clean up any state for any ongoing navigation.
422 void CleanUpNavigation();
424 // PlzNavigate
425 // Clears the speculative members, returning the RenderFrameHost to the caller
426 // for disposal.
427 scoped_ptr<RenderFrameHostImpl> UnsetSpeculativeRenderFrameHost();
429 // Notification methods to tell this RenderFrameHostManager that the frame it
430 // is responsible for has started or stopped loading a document.
431 void OnDidStartLoading();
432 void OnDidStopLoading();
434 // Send updated frame name to all frame proxies when the frame changes its
435 // window.name property.
436 void OnDidUpdateName(const std::string& name);
438 // Send updated origin to all frame proxies when the frame navigates to a new
439 // origin.
440 void OnDidUpdateOrigin(const url::Origin& origin);
442 void EnsureRenderViewInitialized(RenderViewHostImpl* render_view_host,
443 SiteInstance* instance);
445 // Recursively creates swapped out RenderViews and RenderFrameProxies for
446 // this frame's FrameTree and for its opener chain in the given SiteInstance.
447 // This allows other tabs to send cross-process JavaScript calls to their
448 // opener(s) and to any other frames in the opener's FrameTree (e.g.,
449 // supporting calls like window.opener.opener.frames[x][y]).
450 void CreateOpenerProxies(SiteInstance* instance);
452 // Ensure that this frame has proxies in all SiteInstances that can discover
453 // this frame by name (e.g., via window.open("", "frame_name")). See
454 // https://crbug.com/511474.
455 void CreateProxiesForNewNamedFrame();
457 // Returns a routing ID for the current FrameTreeNode's opener node in the
458 // given SiteInstance. May return a routing ID of either a RenderFrameHost
459 // (if opener's current or pending RFH has SiteInstance |instance|) or a
460 // RenderFrameProxyHost. Returns MSG_ROUTING_NONE if there is no opener, or
461 // if the opener node doesn't have a proxy for |instance|.
462 int GetOpenerRoutingID(SiteInstance* instance);
464 // Called on the RFHM of the inner WebContents to create a
465 // RenderFrameProxyHost in its outer WebContents's SiteInstance,
466 // |outer_contents_site_instance|. The frame in outer WebContents that is
467 // hosting the inner WebContents is |render_frame_host|, and the frame will
468 // be swapped out with the proxy.
469 void CreateOuterDelegateProxy(SiteInstance* outer_contents_site_instance,
470 RenderFrameHostImpl* render_frame_host);
472 // Sets the child RenderWidgetHostView for this frame, which must be part of
473 // an inner WebContents.
474 void SetRWHViewForInnerContents(RenderWidgetHostView* child_rwhv);
476 // Returns a copy of the map of proxy hosts. The keys are SiteInstance IDs,
477 // the values are RenderFrameProxyHosts.
478 std::map<int, RenderFrameProxyHost*> GetAllProxyHostsForTesting();
480 private:
481 friend class NavigatorTestWithBrowserSideNavigation;
482 friend class RenderFrameHostManagerTest;
483 friend class TestWebContents;
485 // Stores information regarding a SiteInstance targeted at a specific URL to
486 // allow for comparisons without having to actually create new instances. It
487 // can point to an existing one or store the details needed to create a new
488 // one.
489 struct CONTENT_EXPORT SiteInstanceDescriptor {
490 explicit SiteInstanceDescriptor(content::SiteInstance* site_instance)
491 : existing_site_instance(site_instance),
492 new_is_related_to_current(false) {}
494 SiteInstanceDescriptor(BrowserContext* browser_context,
495 GURL dest_url,
496 bool related_to_current);
498 // Set with an existing SiteInstance to be reused.
499 content::SiteInstance* existing_site_instance;
501 // In case |existing_site_instance| is null, specify a new site URL.
502 GURL new_site_url;
504 // In case |existing_site_instance| is null, specify if the new site should
505 // be created in a new BrowsingInstance or not.
506 bool new_is_related_to_current;
509 // Used with FrameTree::ForEach to erase RenderFrameProxyHosts from a
510 // FrameTreeNode's RenderFrameHostManager.
511 static bool ClearProxiesInSiteInstance(int32 site_instance_id,
512 FrameTreeNode* node);
513 // Used with FrameTree::ForEach to reset initialized state of
514 // RenderFrameProxyHosts from a FrameTreeNode's RenderFrameHostManager.
515 static bool ResetProxiesInSiteInstance(int32 site_instance_id,
516 FrameTreeNode* node);
518 // Returns whether this tab should transition to a new renderer for
519 // cross-site URLs. Enabled unless we see the --process-per-tab command line
520 // switch. Can be overridden in unit tests.
521 bool ShouldTransitionCrossSite();
523 // Returns true if for the navigation from |current_effective_url| to
524 // |new_effective_url|, a new SiteInstance and BrowsingInstance should be
525 // created (even if we are in a process model that doesn't usually swap).
526 // This forces a process swap and severs script connections with existing
527 // tabs. Cases where this can happen include transitions between WebUI and
528 // regular web pages. |new_site_instance| may be null.
529 // If there is no current NavigationEntry, then |current_is_view_source_mode|
530 // should be the same as |new_is_view_source_mode|.
532 // We use the effective URL here, since that's what is used in the
533 // SiteInstance's site and when we later call IsSameWebSite. If there is no
534 // current NavigationEntry, check the current SiteInstance's site, which might
535 // already be committed to a Web UI URL (such as the NTP).
536 bool ShouldSwapBrowsingInstancesForNavigation(
537 const GURL& current_effective_url,
538 bool current_is_view_source_mode,
539 SiteInstance* new_site_instance,
540 const GURL& new_effective_url,
541 bool new_is_view_source_mode) const;
543 // Creates a new Web UI, ensuring that the bindings are appropriate compared
544 // to |bindings|.
545 scoped_ptr<WebUIImpl> CreateWebUI(const GURL& url, int bindings);
547 // Returns true if it is safe to reuse the current WebUI when navigating from
548 // |current_entry| to |new_url|.
549 bool ShouldReuseWebUI(
550 const NavigationEntry* current_entry,
551 const GURL& new_url) const;
553 // Returns the SiteInstance to use for the navigation.
554 SiteInstance* GetSiteInstanceForNavigation(const GURL& dest_url,
555 SiteInstance* source_instance,
556 SiteInstance* dest_instance,
557 SiteInstance* candidate_instance,
558 ui::PageTransition transition,
559 bool dest_is_restore,
560 bool dest_is_view_source_mode);
562 // Returns a descriptor of the appropriate SiteInstance object for the given
563 // |dest_url|, possibly reusing the current, source or destination
564 // SiteInstance. The actual SiteInstance can then be obtained calling
565 // ConvertToSiteInstance with the descriptor.
567 // |source_instance| is the SiteInstance of the frame that initiated the
568 // navigation. |current_instance| is the SiteInstance of the frame that is
569 // currently navigating. |dest_instance| is a predetermined SiteInstance that
570 // will be used if not null.
571 // For example, if you have a parent frame A, which has a child frame B, and
572 // A is trying to change the src attribute of B, this will cause a navigation
573 // where the source SiteInstance is A and B is the current SiteInstance.
575 // This is a helper function for GetSiteInstanceForNavigation.
576 SiteInstanceDescriptor DetermineSiteInstanceForURL(
577 const GURL& dest_url,
578 SiteInstance* source_instance,
579 SiteInstance* current_instance,
580 SiteInstance* dest_instance,
581 ui::PageTransition transition,
582 bool dest_is_restore,
583 bool dest_is_view_source_mode,
584 bool force_browsing_instance_swap);
586 // Converts a SiteInstanceDescriptor to the actual SiteInstance it describes.
587 // If a |candidate_instance| is provided (is not nullptr) and it matches the
588 // description, it is returned as is.
589 SiteInstance* ConvertToSiteInstance(const SiteInstanceDescriptor& descriptor,
590 SiteInstance* candidate_instance);
592 // Determines the appropriate url to use as the current url for SiteInstance
593 // selection.
594 const GURL& GetCurrentURLForSiteInstance(
595 SiteInstance* current_instance,
596 NavigationEntry* current_entry);
598 // Creates a new RenderFrameHostImpl for the |new_instance| and assign it to
599 // |pending_render_frame_host_| while respecting the opener route if needed
600 // and stores it in pending_render_frame_host_.
601 void CreatePendingRenderFrameHost(SiteInstance* old_instance,
602 SiteInstance* new_instance,
603 bool is_main_frame);
605 // Ensure that we have created all needed proxies for a new RFH with
606 // SiteInstance |new_instance|: (1) create swapped-out RVHs and proxies for
607 // the new RFH's opener chain if we are staying in the same BrowsingInstance;
608 // (2) Create proxies for the new RFH's SiteInstance in its own frame tree;
609 // (3) set any additional flags for the new RenderFrame with
610 // |create_render_frame_flags|.
611 void CreateProxiesForNewRenderFrameHost(SiteInstance* old_instance,
612 SiteInstance* new_instance,
613 int* create_render_frame_flags);
615 // Create swapped out RenderViews and RenderFrameProxies in the given
616 // SiteInstance for all frames on the opener chain of this frame. Same as
617 // CreateOpenerProxies, but starts from this frame's opener, calling
618 // CreateOpenerProxies on it if it exists and returning otherwise.
619 void CreateOpenerProxiesIfNeeded(SiteInstance* instance);
621 // Creates a RenderFrameHost and corresponding RenderViewHost if necessary.
622 scoped_ptr<RenderFrameHostImpl> CreateRenderFrameHost(SiteInstance* instance,
623 int view_routing_id,
624 int frame_routing_id,
625 int flags);
627 // PlzNavigate
628 // Creates and initializes a speculative RenderFrameHost and/or WebUI for an
629 // ongoing navigation. They might be destroyed and re-created later if the
630 // navigation is redirected to a different SiteInstance.
631 bool CreateSpeculativeRenderFrameHost(const GURL& url,
632 SiteInstance* old_instance,
633 SiteInstance* new_instance,
634 int bindings);
636 // Sets up the necessary state for a new RenderViewHost. Creates a
637 // RenderFrameProxy in the target renderer process with the given
638 // |proxy_routing_id|, which is used to route IPC messages when in swapped
639 // out state. Returns early if the RenderViewHost has already been
640 // initialized for another RenderFrameHost.
641 bool InitRenderView(RenderViewHostImpl* render_view_host,
642 int proxy_routing_id,
643 bool for_main_frame_navigation);
645 // Initialization for RenderFrameHost uses the same sequence as InitRenderView
646 // above.
647 bool InitRenderFrame(RenderFrameHostImpl* render_frame_host);
649 // Sets the pending RenderFrameHost/WebUI to be the active one. Note that this
650 // doesn't require the pending render_frame_host_ pointer to be non-NULL,
651 // since there could be Web UI switching as well. Call this for every commit.
652 // If PlzNavigate is enabled the method will set the speculative (not pending)
653 // RenderFrameHost to be the active one.
654 void CommitPending();
656 // Helper to call CommitPending() in all necessary cases.
657 void CommitPendingIfNecessary(RenderFrameHostImpl* render_frame_host,
658 bool was_caused_by_user_gesture);
660 // Commits any pending sandbox flag updates when the renderer's frame
661 // navigates.
662 void CommitPendingSandboxFlags();
664 // Runs the unload handler in the old RenderFrameHost, after the new
665 // RenderFrameHost has committed. |old_render_frame_host| will either be
666 // deleted or put on the pending delete list during this call.
667 void SwapOutOldFrame(scoped_ptr<RenderFrameHostImpl> old_render_frame_host);
669 // Discards a RenderFrameHost that was never made active (for active ones
670 // SwapOutOldFrame is used instead).
671 void DiscardUnusedFrame(scoped_ptr<RenderFrameHostImpl> render_frame_host);
673 // Holds |render_frame_host| until it can be deleted when its swap out ACK
674 // arrives.
675 void MoveToPendingDeleteHosts(
676 scoped_ptr<RenderFrameHostImpl> render_frame_host);
678 // If |render_frame_host| is the last remaining active frame in its
679 // SiteInstance, this will shutdown all the RenderFrameProxyHosts in the
680 // SiteInstance. This is appropriate if |render_frame_host| is about to be
681 // destroyed.
682 void ShutdownProxiesIfLastActiveFrameInSiteInstance(
683 RenderFrameHostImpl* render_frame_host);
685 // Helper method to terminate the pending RenderFrameHost. The frame may be
686 // deleted immediately, or it may be kept around in hopes of later reuse.
687 void CancelPending();
689 // Clears pending_render_frame_host_, returning it to the caller for disposal.
690 scoped_ptr<RenderFrameHostImpl> UnsetPendingRenderFrameHost();
692 // Helper method to set the active RenderFrameHost. Returns the old
693 // RenderFrameHost and updates counts.
694 scoped_ptr<RenderFrameHostImpl> SetRenderFrameHost(
695 scoped_ptr<RenderFrameHostImpl> render_frame_host);
697 RenderFrameHostImpl* UpdateStateForNavigate(
698 const GURL& dest_url,
699 SiteInstance* source_instance,
700 SiteInstance* dest_instance,
701 ui::PageTransition transition,
702 bool dest_is_restore,
703 bool dest_is_view_source_mode,
704 const GlobalRequestID& transferred_request_id,
705 int bindings);
707 // Called when a renderer process is starting to close. We should not
708 // schedule new navigations in its swapped out RenderFrameHosts after this.
709 void RendererProcessClosing(RenderProcessHost* render_process_host);
711 // For use in creating RenderFrameHosts.
712 FrameTreeNode* frame_tree_node_;
714 // Our delegate, not owned by us. Guaranteed non-NULL.
715 Delegate* delegate_;
717 // Implemented by the owner of this class. These delegates are installed into
718 // all the RenderFrameHosts that we create.
719 RenderFrameHostDelegate* render_frame_delegate_;
720 RenderViewHostDelegate* render_view_delegate_;
721 RenderWidgetHostDelegate* render_widget_delegate_;
723 // Our RenderFrameHost and its associated Web UI (if any, will be NULL for
724 // non-WebUI pages). This object is responsible for all communication with
725 // a child RenderFrame instance.
726 // For now, RenderFrameHost keeps a RenderViewHost in its SiteInstance alive.
727 // Eventually, RenderViewHost will be replaced with a page context.
728 scoped_ptr<RenderFrameHostImpl> render_frame_host_;
729 scoped_ptr<WebUIImpl> web_ui_;
731 // A RenderFrameHost used to load a cross-site page. This remains hidden
732 // while a cross-site request is pending until it calls DidNavigate. It may
733 // have an associated Web UI, in which case the Web UI pointer will be non-
734 // NULL.
736 // The |pending_web_ui_| may be non-NULL even when the
737 // |pending_render_frame_host_| is NULL. This will happen when we're
738 // transitioning between two Web UI pages: the RFH won't be swapped, so the
739 // pending pointer will be unused, but there will be a pending Web UI
740 // associated with the navigation.
741 // Note: This is not used in PlzNavigate.
742 scoped_ptr<RenderFrameHostImpl> pending_render_frame_host_;
744 // If a pending request needs to be transferred to another process, this
745 // owns the request until it's transferred to the new process, so it will be
746 // cleaned up if the navigation is cancelled. Otherwise, this is NULL.
747 scoped_ptr<CrossSiteTransferringRequest> cross_site_transferring_request_;
749 // This is used to temporarily store the NavigationHandle during
750 // transferring navigations. The handle needs to be stored because the old
751 // RenderFrameHost may be discarded before a new RenderFrameHost is created
752 // for the navigation.
753 // PlzNavigate: this will never be set since there are no transferring
754 // navigations in PlzNavigate.
755 scoped_ptr<NavigationHandleImpl> transfer_navigation_handle_;
757 // If either of these is non-NULL, the pending navigation is to a chrome:
758 // page. The scoped_ptr is used if pending_web_ui_ != web_ui_, the WeakPtr is
759 // used for when they reference the same object. If either is non-NULL, the
760 // other should be NULL.
761 // Note: These are not used in PlzNavigate.
762 scoped_ptr<WebUIImpl> pending_web_ui_;
763 base::WeakPtr<WebUIImpl> pending_and_current_web_ui_;
765 class RenderFrameProxyHostMap;
766 scoped_ptr<RenderFrameProxyHostMap> proxy_hosts_;
768 // A list of RenderFrameHosts waiting to shut down after swapping out. We use
769 // a linked list since we expect frequent deletes and no indexed access, and
770 // because sets don't appear to support linked_ptrs.
771 typedef std::list<linked_ptr<RenderFrameHostImpl> > RFHPendingDeleteList;
772 RFHPendingDeleteList pending_delete_hosts_;
774 // The intersitial page currently shown if any, not own by this class
775 // (the InterstitialPage is self-owned, it deletes itself when hidden).
776 InterstitialPageImpl* interstitial_page_;
778 // PlzNavigate
779 // These members store a speculative RenderFrameHost and WebUI. They are
780 // created early in a navigation so a renderer process can be started in
781 // parallel, if needed. This is purely a performance optimization and is not
782 // required for correct behavior. The created RenderFrameHost might be
783 // discarded later on if the final URL's SiteInstance isn't compatible with
784 // what was used to create it.
785 // Note: PlzNavigate only uses speculative RenderFrameHost and WebUI, not
786 // the pending ones.
787 scoped_ptr<RenderFrameHostImpl> speculative_render_frame_host_;
788 scoped_ptr<WebUIImpl> speculative_web_ui_;
790 // PlzNavigate
791 // If true at navigation commit time the current WebUI will be kept instead of
792 // creating a new one.
793 bool should_reuse_web_ui_;
795 base::WeakPtrFactory<RenderFrameHostManager> weak_factory_;
797 DISALLOW_COPY_AND_ASSIGN(RenderFrameHostManager);
800 } // namespace content
802 #endif // CONTENT_BROWSER_FRAME_HOST_RENDER_FRAME_HOST_MANAGER_H_