Battery Status API: add UMA logging for Linux.
[chromium-blink-merge.git] / content / browser / frame_host / render_frame_host_manager.h
blob48a5f24b6f4e79f999567b0770c82fd6565c22ae
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;
21 struct FrameMsg_Navigate_Params;
23 namespace content {
24 class BrowserContext;
25 class CrossProcessFrameConnector;
26 class CrossSiteTransferringRequest;
27 class InterstitialPageImpl;
28 class FrameTreeNode;
29 class NavigationControllerImpl;
30 class NavigationEntry;
31 class NavigationEntryImpl;
32 class NavigationRequest;
33 class RenderFrameHost;
34 class RenderFrameHostDelegate;
35 class RenderFrameHost;
36 class RenderFrameHostImpl;
37 class RenderFrameHostManagerTest;
38 class RenderFrameProxyHost;
39 class RenderViewHost;
40 class RenderViewHostImpl;
41 class RenderWidgetHostDelegate;
42 class RenderWidgetHostView;
43 class TestWebContents;
44 class WebUIImpl;
45 struct NavigationBeforeCommitInfo;
47 // Manages RenderFrameHosts for a FrameTreeNode. This class acts as a state
48 // machine to make cross-process navigations in a frame possible.
49 class CONTENT_EXPORT RenderFrameHostManager : public NotificationObserver {
50 public:
51 // Functions implemented by our owner that we need.
53 // TODO(brettw) Clean this up! These are all the functions in WebContentsImpl
54 // that are required to run this class. The design should probably be better
55 // such that these are more clear.
57 // There is additional complexity that some of the functions we need in
58 // WebContentsImpl are inherited and non-virtual. These are named with
59 // "RenderManager" so that the duplicate implementation of them will be clear.
61 // Functions and parameters whose description are prefixed by PlzNavigate are
62 // part of a navigation refactoring project, currently behind the
63 // enable-browser-side-navigation flag. The idea is to move the logic behind
64 // driving navigations from the renderer to the browser.
65 class CONTENT_EXPORT Delegate {
66 public:
67 // Initializes the given renderer if necessary and creates the view ID
68 // corresponding to this view host. If this method is not called and the
69 // process is not shared, then the WebContentsImpl will act as though the
70 // renderer is not running (i.e., it will render "sad tab"). This method is
71 // automatically called from LoadURL. |for_main_frame_navigation| indicates
72 // whether this RenderViewHost is used to render a top-level frame, so the
73 // appropriate RenderWidgetHostView type is used.
74 virtual bool CreateRenderViewForRenderManager(
75 RenderViewHost* render_view_host,
76 int opener_route_id,
77 int proxy_routing_id,
78 bool for_main_frame_navigation) = 0;
79 virtual bool CreateRenderFrameForRenderManager(
80 RenderFrameHost* render_frame_host,
81 int parent_routing_id) = 0;
82 virtual void BeforeUnloadFiredFromRenderManager(
83 bool proceed, const base::TimeTicks& proceed_time,
84 bool* proceed_to_fire_unload) = 0;
85 virtual void RenderProcessGoneFromRenderManager(
86 RenderViewHost* render_view_host) = 0;
87 virtual void UpdateRenderViewSizeForRenderManager() = 0;
88 virtual void CancelModalDialogsForRenderManager() = 0;
89 virtual void NotifySwappedFromRenderManager(RenderFrameHost* old_host,
90 RenderFrameHost* new_host,
91 bool is_main_frame) = 0;
92 virtual NavigationControllerImpl&
93 GetControllerForRenderManager() = 0;
95 // Create swapped out RenderViews in the given SiteInstance for each tab in
96 // the opener chain of this tab, if any. This allows the current tab to
97 // make cross-process script calls to its opener(s). Returns the route ID
98 // of the immediate opener, if one exists (otherwise MSG_ROUTING_NONE).
99 virtual int CreateOpenerRenderViewsForRenderManager(
100 SiteInstance* instance) = 0;
102 // Creates a WebUI object for the given URL if one applies. Ownership of the
103 // returned pointer will be passed to the caller. If no WebUI applies,
104 // returns NULL.
105 virtual WebUIImpl* CreateWebUIForRenderManager(const GURL& url) = 0;
107 // Returns the navigation entry of the current navigation, or NULL if there
108 // is none.
109 virtual NavigationEntry*
110 GetLastCommittedNavigationEntryForRenderManager() = 0;
112 // Returns true if the location bar should be focused by default rather than
113 // the page contents. The view calls this function when the tab is focused
114 // to see what it should do.
115 virtual bool FocusLocationBarByDefault() = 0;
117 // Focuses the location bar.
118 virtual void SetFocusToLocationBar(bool select_all) = 0;
120 // Creates a view and sets the size for the specified RVH.
121 virtual void CreateViewAndSetSizeForRVH(RenderViewHost* rvh) = 0;
123 // Returns true if views created for this delegate should be created in a
124 // hidden state.
125 virtual bool IsHidden() = 0;
127 protected:
128 virtual ~Delegate() {}
131 // Used with FrameTree::ForEach to delete RenderFrameHosts pending shutdown
132 // from a FrameTreeNode's RenderFrameHostManager. Used during destruction of
133 // WebContentsImpl.
134 static bool ClearRFHsPendingShutdown(FrameTreeNode* node);
136 // All three delegate pointers must be non-NULL and are not owned by this
137 // class. They must outlive this class. The RenderViewHostDelegate and
138 // RenderWidgetHostDelegate are what will be installed into all
139 // RenderViewHosts that are created.
141 // You must call Init() before using this class.
142 RenderFrameHostManager(
143 FrameTreeNode* frame_tree_node,
144 RenderFrameHostDelegate* render_frame_delegate,
145 RenderViewHostDelegate* render_view_delegate,
146 RenderWidgetHostDelegate* render_widget_delegate,
147 Delegate* delegate);
148 virtual ~RenderFrameHostManager();
150 // For arguments, see WebContentsImpl constructor.
151 void Init(BrowserContext* browser_context,
152 SiteInstance* site_instance,
153 int view_routing_id,
154 int frame_routing_id);
156 // Returns the currently active RenderFrameHost.
158 // This will be non-NULL between Init() and Shutdown(). You may want to NULL
159 // check it in many cases, however. Windows can send us messages during the
160 // destruction process after it has been shut down.
161 RenderFrameHostImpl* current_frame_host() const {
162 return render_frame_host_.get();
165 // TODO(creis): Remove this when we no longer use RVH for navigation.
166 RenderViewHostImpl* current_host() const;
168 // Returns the view associated with the current RenderViewHost, or NULL if
169 // there is no current one.
170 RenderWidgetHostView* GetRenderWidgetHostView() const;
172 RenderFrameProxyHost* GetProxyToParent();
174 // Returns the pending RenderFrameHost, or NULL if there is no pending one.
175 RenderFrameHostImpl* pending_frame_host() const {
176 return pending_render_frame_host_.get();
179 // TODO(creis): Remove this when we no longer use RVH for navigation.
180 RenderViewHostImpl* pending_render_view_host() const;
182 // Returns the current committed Web UI or NULL if none applies.
183 WebUIImpl* web_ui() const { return web_ui_.get(); }
185 // Returns the Web UI for the pending navigation, or NULL of none applies.
186 WebUIImpl* pending_web_ui() const {
187 return pending_web_ui_.get() ? pending_web_ui_.get() :
188 pending_and_current_web_ui_.get();
191 // Sets the pending Web UI for the pending navigation, ensuring that the
192 // bindings are appropriate for the given NavigationEntry.
193 void SetPendingWebUI(const NavigationEntryImpl& entry);
195 // Called when we want to instruct the renderer to navigate to the given
196 // navigation entry. It may create a new RenderFrameHost or re-use an existing
197 // one. The RenderFrameHost to navigate will be returned. Returns NULL if one
198 // could not be created.
199 RenderFrameHostImpl* Navigate(const NavigationEntryImpl& entry);
201 // Instructs the various live views to stop. Called when the user directed the
202 // page to stop loading.
203 void Stop();
205 // Notifies the regular and pending RenderViewHosts that a load is or is not
206 // happening. Even though the message is only for one of them, we don't know
207 // which one so we tell both.
208 void SetIsLoading(bool is_loading);
210 // Whether to close the tab or not when there is a hang during an unload
211 // handler. If we are mid-crosssite navigation, then we should proceed
212 // with the navigation instead of closing the tab.
213 bool ShouldCloseTabOnUnresponsiveRenderer();
215 // Confirms whether we should close the page or navigate away. This is called
216 // before a cross-site request or before a tab/window is closed (as indicated
217 // by the first parameter) to allow the appropriate renderer to approve or
218 // deny the request. |proceed| indicates whether the user chose to proceed.
219 // |proceed_time| is the time when the request was allowed to proceed.
220 void OnBeforeUnloadACK(bool for_cross_site_transition,
221 bool proceed,
222 const base::TimeTicks& proceed_time);
224 // The |pending_render_frame_host| is ready to commit a page. We should
225 // ensure that the old RenderFrameHost runs its unload handler first and
226 // determine whether a RenderFrameHost transfer is needed.
227 // |cross_site_transferring_request| is NULL if a request is not being
228 // transferred between renderers.
229 void OnCrossSiteResponse(
230 RenderFrameHostImpl* pending_render_frame_host,
231 const GlobalRequestID& global_request_id,
232 scoped_ptr<CrossSiteTransferringRequest> cross_site_transferring_request,
233 const std::vector<GURL>& transfer_url_chain,
234 const Referrer& referrer,
235 PageTransition page_transition,
236 bool should_replace_current_entry);
238 // Received a response from CrossSiteResourceHandler. If the navigation
239 // specifies a transition, this is called and the navigation will not resume
240 // until ResumeResponseDeferredAtStart.
241 void OnDeferredAfterResponseStarted(
242 const GlobalRequestID& global_request_id,
243 RenderFrameHostImpl* pending_render_frame_host);
245 // Resume navigation paused after receiving response headers.
246 void ResumeResponseDeferredAtStart();
248 // Called when a renderer's frame navigates.
249 void DidNavigateFrame(RenderFrameHostImpl* render_frame_host);
251 // Called when a renderer sets its opener to null.
252 void DidDisownOpener(RenderFrameHost* render_frame_host);
254 // Helper method to create and initialize a RenderFrameHost. If |swapped_out|
255 // is true, it will be initially placed on the swapped out hosts list.
256 // Returns the routing id of the *view* associated with the frame.
257 int CreateRenderFrame(SiteInstance* instance,
258 int opener_route_id,
259 bool swapped_out,
260 bool for_main_frame_navigation,
261 bool hidden);
263 // Helper method to create and initialize a RenderFrameProxyHost and return
264 // its routing id.
265 int CreateRenderFrameProxy(SiteInstance* instance);
267 // Sets the passed passed interstitial as the currently showing interstitial.
268 // |interstitial_page| should be non NULL (use the remove_interstitial_page
269 // method to unset the interstitial) and no interstitial page should be set
270 // when there is already a non NULL interstitial page set.
271 void set_interstitial_page(InterstitialPageImpl* interstitial_page) {
272 DCHECK(!interstitial_page_ && interstitial_page);
273 interstitial_page_ = interstitial_page;
276 // Unsets the currently showing interstitial.
277 void remove_interstitial_page() {
278 DCHECK(interstitial_page_);
279 interstitial_page_ = NULL;
282 // Returns the currently showing interstitial, NULL if no interstitial is
283 // showing.
284 InterstitialPageImpl* interstitial_page() const { return interstitial_page_; }
286 // NotificationObserver implementation.
287 virtual void Observe(int type,
288 const NotificationSource& source,
289 const NotificationDetails& details) OVERRIDE;
291 // Returns whether the given RenderFrameHost (or its associated
292 // RenderViewHost) is on the list of swapped out RenderFrameHosts.
293 bool IsRVHOnSwappedOutList(RenderViewHostImpl* rvh) const;
294 bool IsOnSwappedOutList(RenderFrameHostImpl* rfh) const;
296 // Returns the swapped out RenderViewHost or RenderFrameHost for the given
297 // SiteInstance, if any. This method is *deprecated* and
298 // GetRenderFrameProxyHost should be used.
299 RenderViewHostImpl* GetSwappedOutRenderViewHost(SiteInstance* instance) const;
300 RenderFrameProxyHost* GetRenderFrameProxyHost(
301 SiteInstance* instance) const;
303 // Deletes a RenderFrameHost that was pending shutdown.
304 void ClearPendingShutdownRFHForSiteInstance(int32 site_instance_id,
305 RenderFrameHostImpl* rfh);
307 // Deletes any proxy hosts associated with this node. Used during destruction
308 // of WebContentsImpl.
309 void ResetProxyHosts();
311 // Returns the routing id for a RenderFrameHost or RenderFrameHostProxy
312 // that has the given SiteInstance and is associated with this
313 // RenderFrameHostManager. Returns MSG_ROUTING_NONE if none is found.
314 int GetRoutingIdForSiteInstance(SiteInstance* site_instance);
316 // PlzNavigate: sends a RequestNavigation IPC to the renderer to ask it to
317 // navigate. If no live renderer is present, then the navigation request will
318 // be sent directly to the ResourceDispatcherHost.
319 bool RequestNavigation(const NavigationEntryImpl& entry,
320 const FrameMsg_Navigate_Params& navigate_params);
322 // PlzNavigate: Used to start a navigation. OnBeginNavigation is called
323 // directly by RequestNavigation when there is no live renderer. Otherwise, it
324 // is called following a BeginNavigation IPC from the renderer (which in
325 // browser-initiated navigation also happens after RequestNavigation has been
326 // called).
327 void OnBeginNavigation(const FrameHostMsg_BeginNavigation_Params& params);
329 // PlzNavigate: Called when a navigation request has received a response, to
330 // select a renderer to use for the navigation.
331 void CommitNavigation(const NavigationBeforeCommitInfo& info);
333 private:
334 friend class RenderFrameHostManagerTest;
335 friend class TestWebContents;
337 FRIEND_TEST_ALL_PREFIXES(CrossProcessFrameTreeBrowserTest,
338 CreateCrossProcessSubframeProxies);
340 // Returns the current navigation request (used in the PlzNavigate navigation
341 // logic refactoring project).
342 NavigationRequest* navigation_request_for_testing() const {
343 return navigation_request_.get(); }
345 // Used with FrameTree::ForEach to erase RenderFrameProxyHosts from a
346 // FrameTreeNode's RenderFrameHostManager.
347 static bool ClearProxiesInSiteInstance(int32 site_instance_id,
348 FrameTreeNode* node);
350 // Returns whether this tab should transition to a new renderer for
351 // cross-site URLs. Enabled unless we see the --process-per-tab command line
352 // switch. Can be overridden in unit tests.
353 bool ShouldTransitionCrossSite();
355 // Returns true if for the navigation from |current_effective_url| to
356 // |new_effective_url|, a new SiteInstance and BrowsingInstance should be
357 // created (even if we are in a process model that doesn't usually swap).
358 // This forces a process swap and severs script connections with existing
359 // tabs. Cases where this can happen include transitions between WebUI and
360 // regular web pages. |new_site_instance| may be null.
361 // If there is no current NavigationEntry, then |current_is_view_source_mode|
362 // should be the same as |new_is_view_source_mode|.
364 // We use the effective URL here, since that's what is used in the
365 // SiteInstance's site and when we later call IsSameWebSite. If there is no
366 // current NavigationEntry, check the current SiteInstance's site, which might
367 // already be committed to a Web UI URL (such as the NTP).
368 bool ShouldSwapBrowsingInstancesForNavigation(
369 const GURL& current_effective_url,
370 bool current_is_view_source_mode,
371 SiteInstance* new_site_instance,
372 const GURL& new_effective_url,
373 bool new_is_view_source_mode) const;
375 // Returns true if it is safe to reuse the current WebUI when navigating from
376 // |current_entry| to |new_entry|.
377 bool ShouldReuseWebUI(
378 const NavigationEntry* current_entry,
379 const NavigationEntryImpl* new_entry) const;
381 // Returns the SiteInstance to use for the navigation.
382 SiteInstance* GetSiteInstanceForNavigation(
383 const GURL& dest_url,
384 SiteInstance* dest_instance,
385 PageTransition dest_transition,
386 bool dest_is_restore,
387 bool dest_is_view_source_mode);
389 // Returns an appropriate SiteInstance object for the given |dest_url|,
390 // possibly reusing the current SiteInstance. If --process-per-tab is used,
391 // this is only called when ShouldSwapBrowsingInstancesForNavigation returns
392 // true. |dest_instance| will be used if it is not null.
393 // This is a helper function for GetSiteInstanceForNavigation.
394 SiteInstance* GetSiteInstanceForURL(
395 const GURL& dest_url,
396 SiteInstance* dest_instance,
397 PageTransition dest_transition,
398 bool dest_is_restore,
399 bool dest_is_view_source_mode,
400 SiteInstance* current_instance,
401 bool force_browsing_instance_swap);
403 // Creates a new RenderFrameHostImpl for the |new_instance| while respecting
404 // the opener route if needed and stores it in pending_render_frame_host_.
405 void CreateRenderFrameHostForNewSiteInstance(
406 SiteInstance* old_instance,
407 SiteInstance* new_instance,
408 bool is_main_frame);
410 // Creates a RenderFrameHost and corresponding RenderViewHost if necessary.
411 scoped_ptr<RenderFrameHostImpl> CreateRenderFrameHost(SiteInstance* instance,
412 int view_routing_id,
413 int frame_routing_id,
414 bool swapped_out,
415 bool hidden);
417 // Sets up the necessary state for a new RenderViewHost with the given opener,
418 // if necessary. It creates a RenderFrameProxy in the target renderer process
419 // with the given |proxy_routing_id|, which is used to route IPC messages when
420 // in swapped out state. Returns early if the RenderViewHost has already been
421 // initialized for another RenderFrameHost.
422 // TODO(creis): opener_route_id is currently for the RenderViewHost but should
423 // be for the RenderFrame, since frames can have openers.
424 bool InitRenderView(RenderViewHost* render_view_host,
425 int opener_route_id,
426 int proxy_routing_id,
427 bool for_main_frame_navigation);
429 // Initialization for RenderFrameHost uses the same sequence as InitRenderView
430 // above.
431 bool InitRenderFrame(RenderFrameHost* render_frame_host);
433 // Sets the pending RenderFrameHost/WebUI to be the active one. Note that this
434 // doesn't require the pending render_frame_host_ pointer to be non-NULL,
435 // since there could be Web UI switching as well. Call this for every commit.
436 void CommitPending();
438 // Runs the unload handler in the current page, after the new page has
439 // committed.
440 void SwapOutOldPage(RenderFrameHostImpl* old_render_frame_host);
442 // Shutdown all RenderFrameProxyHosts in a SiteInstance. This is called to
443 // shutdown frames when all the frames in a SiteInstance are confirmed to be
444 // swapped out.
445 void ShutdownRenderFrameProxyHostsInSiteInstance(int32 site_instance_id);
447 // Helper method to terminate the pending RenderViewHost.
448 void CancelPending();
450 // Helper method to set the active RenderFrameHost. Returns the old
451 // RenderFrameHost and updates counts.
452 scoped_ptr<RenderFrameHostImpl> SetRenderFrameHost(
453 scoped_ptr<RenderFrameHostImpl> render_frame_host);
455 RenderFrameHostImpl* UpdateStateForNavigate(
456 const NavigationEntryImpl& entry);
458 // Called when a renderer process is starting to close. We should not
459 // schedule new navigations in its swapped out RenderFrameHosts after this.
460 void RendererProcessClosing(RenderProcessHost* render_process_host);
462 // Helper method to delete a RenderFrameProxyHost from the list, if one exists
463 // for the given |instance|.
464 void DeleteRenderFrameProxyHost(SiteInstance* instance);
466 // For use in creating RenderFrameHosts.
467 FrameTreeNode* frame_tree_node_;
469 // Our delegate, not owned by us. Guaranteed non-NULL.
470 Delegate* delegate_;
472 // Whether a navigation requiring different RenderFrameHosts is pending. This
473 // is either for cross-site requests or when required for the process type
474 // (like WebUI).
475 bool cross_navigation_pending_;
477 // Implemented by the owner of this class. These delegates are installed into
478 // all the RenderFrameHosts that we create.
479 RenderFrameHostDelegate* render_frame_delegate_;
480 RenderViewHostDelegate* render_view_delegate_;
481 RenderWidgetHostDelegate* render_widget_delegate_;
483 // Our RenderFrameHost and its associated Web UI (if any, will be NULL for
484 // non-WebUI pages). This object is responsible for all communication with
485 // a child RenderFrame instance.
486 // For now, RenderFrameHost keeps a RenderViewHost in its SiteInstance alive.
487 // Eventually, RenderViewHost will be replaced with a page context.
488 scoped_ptr<RenderFrameHostImpl> render_frame_host_;
489 scoped_ptr<WebUIImpl> web_ui_;
491 // A RenderFrameHost used to load a cross-site page. This remains hidden
492 // while a cross-site request is pending until it calls DidNavigate. It may
493 // have an associated Web UI, in which case the Web UI pointer will be non-
494 // NULL.
496 // The |pending_web_ui_| may be non-NULL even when the
497 // |pending_render_frame_host_| is NULL. This will happen when we're
498 // transitioning between two Web UI pages: the RFH won't be swapped, so the
499 // pending pointer will be unused, but there will be a pending Web UI
500 // associated with the navigation.
501 scoped_ptr<RenderFrameHostImpl> pending_render_frame_host_;
503 // If a pending request needs to be transferred to another process, this
504 // owns the request until it's transferred to the new process, so it will be
505 // cleaned up if the navigation is cancelled. Otherwise, this is NULL.
506 scoped_ptr<CrossSiteTransferringRequest> cross_site_transferring_request_;
508 // Tracks information about any navigation paused after receiving response
509 // headers.
510 scoped_ptr<GlobalRequestID> response_started_id_;
512 // If either of these is non-NULL, the pending navigation is to a chrome:
513 // page. The scoped_ptr is used if pending_web_ui_ != web_ui_, the WeakPtr is
514 // used for when they reference the same object. If either is non-NULL, the
515 // other should be NULL.
516 scoped_ptr<WebUIImpl> pending_web_ui_;
517 base::WeakPtr<WebUIImpl> pending_and_current_web_ui_;
519 // A map of site instance ID to RenderFrameProxyHosts.
520 typedef base::hash_map<int32, RenderFrameProxyHost*> RenderFrameProxyHostMap;
521 RenderFrameProxyHostMap proxy_hosts_;
523 // A map of RenderFrameHosts pending shutdown.
524 typedef base::hash_map<int32, linked_ptr<RenderFrameHostImpl> >
525 RFHPendingDeleteMap;
526 RFHPendingDeleteMap pending_delete_hosts_;
528 // The intersitial page currently shown if any, not own by this class
529 // (the InterstitialPage is self-owned, it deletes itself when hidden).
530 InterstitialPageImpl* interstitial_page_;
532 NotificationRegistrar registrar_;
534 // PlzNavigate: Owns a navigation request that originated in that frame until
535 // it commits.
536 scoped_ptr<NavigationRequest> navigation_request_;
538 base::WeakPtrFactory<RenderFrameHostManager> weak_factory_;
540 DISALLOW_COPY_AND_ASSIGN(RenderFrameHostManager);
543 } // namespace content
545 #endif // CONTENT_BROWSER_FRAME_HOST_RENDER_FRAME_HOST_MANAGER_H_